I'm refactoring a rather large RegExp into a function that returns a RegExp. As a backward-compatibility test, I compared the .source
of the returned RegExp with the .source
of the old RegExp:
getRegExp(/* in the case requiring backward compatibility there's no arguments */)
.source == oldRegExp.source
However, I've noticed that the old RegExp contains various excessive backslashes like [\.\w]
instead of [.\w]
. I'd like to refactor such bits, but there's a number of them and it would be nice to have a similar check (backward compability is not broken). The problem is, /[\.\w]/.source != /[.\w]/.source
. And identifying which backslashes may be removed automatically is not trivial (\.
and .
are not the same outside [...]
and may be in some other cases).
Are you aware of somewhat simple ways to do so? It seems this can only be done by actual parsing of the .source
(compare the example above with /\[\.\w]\/
and /\[.\w]\/
), but may be I'm missing some trick of utilizing browser's built-in properties/methods. The point is, '\"' == '"'
is true, so strings defined with these different syntaxes are stored as "normalized" values ("
), I wonder if such "normalized" pattern is available for a RegExp.