Question

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.

Was it helpful?

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:

  • "Zip or City & State" — the complete matched substring.
  • undefined — the substring captured by the (\\s\\(Optional\\)|$) capture-group. Or not captured, in this case, because that capture-group is inside a non-matching alternand.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top