1

I'm trying to do a non-greedy capture of text inside double quotation marks with regex in node.js. Most of the Google results say I should use one of these:

"(.*?)"
"([^"]*)"

I tried both, but my code doesn't remove the quotes. My code looks so:

var testStr = '|>  "Song" by "Artist" on "Album" <3';
var regex = /"([^"]*)"/g; // or /"(.*?)"/g
var info = testStr.match(regex);
if (info){
    console.dir(info[0]);
    console.dir(info[1]);
    console.dir(info[2]);
}

My output is this:

'"Song"'
'"Artist"'
'"Album"'

What am I doing wrong?

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Loktopus
  • 462
  • 2
  • 12

3 Answers3

2

match method returns an array of just matches. See MDN:

Captured groups are not returned.

A fix is to use exec:

var re = /"(.*?)"/g; 
var str = '|>  "Song" by "Artist" on "Album" <3';
 
while ((m = re.exec(str)) !== null) {
    document.getElementById("res").innerHTML += m[1] + "<br />";
}
<div id="res"/>
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
1

Your regex is looking for, and matching quotes. You can do this to modify the value matched to effectively trim off the quotes.

var testStr = '|>  "Song" by "Artist" on "Album" <3';
var regex = /"([^"]*)"/g; // or /"(.*?)"/g
var info = testStr.match(regex).map(function (o) {
  return o.substr(1,o.length-2)
});
if (info){
    console.dir(info[0]);
    console.dir(info[1]);
    console.dir(info[2]);
}
Regular Jo
  • 5,190
  • 3
  • 25
  • 47
  • I thought about doing this, but I wanted to do it with just one line of regex. – Loktopus May 02 '15 at 20:38
  • 1
    @Didericis Why? `match()` is faster, all other things being equal. http://jsperf.com/match-v-while/3. Even if you choose to stick with stribizhev's answer, you can increase the speed by pushing to an array and dumping rather than writing with each iteration. (I did use a 20k~ long hunk of gibberish with randomly quoted strings as the test-case. The string is the same for all four tests) – Regular Jo May 03 '15 at 01:11
  • I didn't know it was faster. That being said, I still like using just one bit of regex for this. I don't need it to be fast, and I like having everything done in one step instead of two. I won't use use `exec()` when performance is important. Thanks for the info. Do you have a link that explains why it's faster, or do you have a short explanation you could put here? – Loktopus May 03 '15 at 01:25
0

You can give this a shot. This should include everything in the quotes but not the quotes themselves -

/(?<=")[^" ]*(?=")/ 
Tony DiNitto
  • 1,244
  • 1
  • 16
  • 28
  • This is giving me an "invalid group" error: `var regex = /(?<=")[^" ]*(?=")/;` – Loktopus May 02 '15 at 20:25
  • 1
    Strange. This worked for me. Looks like the other solutions here are manipulating the string after the RegEx matches what you have, which might not be a bad idea. – Tony DiNitto May 02 '15 at 20:29
  • Weird. Double checked it, it's definitely the same as what I have above, and it's not working. Tried it with the `g` flag too, same issue. – Loktopus May 02 '15 at 20:37