'"aaa" "bbb"'.match(/("|')[^\1]+\1/g)
// ['"aaa" "bbb"']
'"aaa" "bbb"'.match(/("|')[^"]+\1/g)
// ['"aaa"', '"bbb"']
Why does [^\1]+ instead of [^"]+ make RegExp greedy?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
That isn’t what you think it is doing.
First of all, a
+is always a maximal match, the thing you are calling “greedy”. It’s+?which is a minimal match.Second and more importantly, back‐referencing doesn’t happen in square‐bracketed character classes. You have accidentally just asked for any character except for Control‑A. That’s because backslash followed by digits means that code point in octal notation, as in
\177forDELETEᴀᴋᴀ\x7F, or\40for SPACE ᴀᴋᴀ\x20, or\0for NULL . So when you wrote\1, you have just used U+0001 or\x01. Don’t do that. 🙂You probably mean to use
instead. You’ll need
/smode so that dot can match newlines, which I seem to recall Javascript has some screwed‐up–ness with.EDIT: According to this, clumsy old Javascript has no way to make dot match linebreaks. What rimnods! And of course because Javascript can’t do Unicode regexes, you can’t use the
\p{Any}required by UTS#18’s RL1.2.That means you’ll have to use some lame kludge like
[\S\s]instead if there’s a chance that you might have linebreaks in your quoted strings.