JavaScript Matching quote wrapped strings in javascript with regex

I need a regex for javascript for matching

"{any group of chars}" <-- where that last " is not preceeded by a \

examples:

... foo "bar" ...  => "bar"
... foo"bar\"" ... => "bar\""
... foo "bar" ...  goo"o"ooogle "t\"e\"st"[] => ["bar", "o", "t\"e\"st"]

The actual strings will be longer and may contain multiple matches that could also include white space or regex special chars.

I have started by trying to break down the syntax but not being strong with regex myself I got stuck pretty fast but i did get as far as matching everything except for the case where the match contains \" (i think) ...

https://regex101.com/r/sj4HXw/1

UPDATE:

More about my situation ...

This regex is to be used to "syntax highlight" strings in code blocks embedded in my blog posts so a real world example might look something like this ...

<pre id="test" class="code" data-code="csharp">
   if (ConfigurationManager.AppSettings["LogSql"] == "true")
</pre>

And I am using the following javascript to achieve the highlight ..

var result = $("#test").text().replace(/"[^"\\]*(?:\\[\s\S][^"\\]*)*"/g, "<span class=\"string\">$1</span>");
$("#test").html(result);

For some reason even when the suggested answers (so far at least) are used in this context i'm getting odd results.

This works but puts the value $1 instead of the actual match for some reason.

Answer:1

Simple scenario (as in OP)

The most efficient regex (that is written in accordance with the unroll-the-loop principle) you may use here is

"[^"\\]*(?:\\[\s\S][^"\\]*)*"

See the regex demo

Details:

  • " - match the first "
  • [^"\\]* - 0+ chars other than " and \
  • (?:\\[\s\S][^"\\]*)* - zer or more occurrences of:
    • \\[\s\S] - any char ([\s\S]) with a \ in front
    • [^"\\]* - 0+ chars other than " and \
  • " - a closing ".

Usage:

// MATCHING
var rx = /"[^"\\]*(?:\\[\s\S][^"\\]*)*"/g;
var s = '    ... foo "bar" ...  goo"o"ooogle "t\\"e\\"st"[]';
var res = s.match(rx);
console.log(res);

// REPLACING
console.log(s.replace(rx, '<span>$&</span>'));
Answer:2

Prioritize the escaped characters first:

"(\\.|[^"])*"

https://regex101.com/r/sj4HXw/2

Answer:3

I'm trying to accomplish a couple things with JS/CSS that I can't figure out. Here's what I've got so far: http://codepen.io/melissall/pen/PPWPQE I want the headline to be centered in the image until ...

I'm trying to accomplish a couple things with JS/CSS that I can't figure out. Here's what I've got so far: http://codepen.io/melissall/pen/PPWPQE I want the headline to be centered in the image until ...

  1. css javascript scroll to bottom
  2. css javascript scroll
  3. css js scroll
  4. css js scroll to top
  5. javascript css scroll position
  6. scrollbar css js
  7. css javascript animation scroll
  8. css javascript smooth scroll
  9. css js horizontal scroll
  10. css js smooth scroll
  11. css javascript disable scroll

I have a huge table that PHP gets from a db and displays. Every time php echos a few rows of the table, it flushes the output. So from the client's point of view, the table is basically growing bigger ...

I have a huge table that PHP gets from a db and displays. Every time php echos a few rows of the table, it flushes the output. So from the client's point of view, the table is basically growing bigger ...

  1. javascript flush event queue
  2. javascript flush event

I created a simple snake game after following some simple tutorials on YouTube. The problem is that the game does not have a pause function (e.g. when pressing P the game should pause/resume) and ...

I created a simple snake game after following some simple tutorials on YouTube. The problem is that the game does not have a pause function (e.g. when pressing P the game should pause/resume) and ...

Im trying to update a video's metadata using the Youtube API V3 in my AngularJS app. I am able to upload the video using insert. I had unlimited problems trying to set the video metadata at the same ...

Im trying to update a video's metadata using the Youtube API V3 in my AngularJS app. I am able to upload the video using insert. I had unlimited problems trying to set the video metadata at the same ...