Someone made a suggestion for one of my regexes on the regexlib. The suggestion was I modify the description to include a third option along with the two that were already present. To be honest I didn’t remember if I wanted to allow that third option when I originally wrote to regex, but I looked like I had. So when I tested it I learned something interesting. The expression worked differently for JavaScript than it did for .Net. Normally two regex engines work differently due to syntax, where .Net’s syntax is more robust so the same expression in JavaScript may not be valid syntax so the JavaScript doesn’t work at all. But this wasn’t the case. Not only was the syntax valid but the expression worked. It just didn’t work the same as .Net. The same expression returned different results.
The issue seems to do optional groups. For example the regex
^A(B)?C\1A$
Will match the pattern ABCBA where B is captured and the reference by \1 after the character C. This works as you would expect in both JavaScript and .Net. But if you’ll notice the entire group containing B is optional. This is where the difference occurs
JavaScript will match ACA.
.Net will NOT match ACA.
It seems like in JavaScript the question mark after the group makes the value of the group optional. Meaning the group will also match an empty string or null. So the backreference is matching an empty string also. So basically the backreference becomes optional too.
In .Net the question mark after the group makes group itself optional but it doesn’t capture anything. I think the group itself is empty which isn’t the same as null. The backreference having nothing to match against fails.
I test with a java-based regex engine too and it acted like .Net
This may be something to watch out for especially if you are doing ASP.Net and using the same expression for Client-side and server-side validations.