javascriptregexreluctant-quantifiers

What's wrong with this regex - something to do with the reluctant quantifier?


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?


Solution

  • 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?

    Therefore the first match is just the first ".