Anyone know why this doesn't work in javascript? (tried using Chrome and Firefox):
console.log('"my name is"'.match(/"?(.*?)"?/));
Outputs this:
[""", "", index: 0, input: ""my name is""]
I expected this:
[""my name is"", "my name is", index: 0, input: ""my name is""]
I'm not interested in alternative approaches to solve the problem, and it's not a complete solution anyway for what I was trying to achieve (which I've now done a slightly different way) - I'm just interested in why the match fails.
I expected the reluctant quantifier to match everything up to, but not including the final quote. I don't understand why the expression has failed to match anything?
The problem is that everything in your pattern, including the surrounding quotes, is optional. Meaning it will just as easily match an empty string. So what's going on inside the regex engine?
"?
. No problem it matches the first "
in the string..*?
. Well, the zero-length substring following the first "
matches this so it continues."?
. The next character is m
, so this doesn't match, but that's okay because the last "
is optional, so it just doesn't get captured. Therefore the first match is just the first "
.