regexproof-general

How does RegExp.exec Populate Its Results Array


I'm playing with a some regular expressions and, while looking at some of my matches, I became curious as to why a the exec function produced as many results as it did.

I'm simply seeking a little clarification on the inner workings of the operation so that I can feel more comfortable with why a regular expression is returning n results, instead of just accepting that it does.

Ex.

var invalidValues = new RegExp(
"\\bZIP or City & State$|" +
"\\bCity & State or ZIP$|" +
"\\bEm[ai][ia]l Address(\\s\\(Optional\\)|$)|" +
"^$", "gi"); 

invalidValues.exec("Zip or City & State");
//returns ["Zip or City & State", undefined]

In the example above, I get why it's matching "Zip or City & State", but I don't know why a second match is being produced with an undefined value.

Thanks in advance.


Solution

  • I'm not familiar with Proof General, but it looks as though exec returns only a single match at a time. The results that you're seeing are:

    To remove the latter, you can (presumably) change the ( to (?:, marking it as a non-capturing group.

    To retrieve subsequent matches, you likely need to call exec multiple times.