Description
ES5 has the following to say:
The characters / or backslash \ occurring in the pattern shall be escaped in S as necessary to ensure that the String value formed by concatenating the Strings "/", S, "/", and F can be parsed (in an appropriate lexical context) as a RegularExpressionLiteral that behaves identically to the constructed regular expression. For example, if P is "/", then S could be "/" or "\u002F", among other possibilities, but not "/", because /// followed by F would be parsed as a SingleLineComment rather than a RegularExpressionLiteral. If P is the empty String, this specification can be met by letting S be "(?:)"
The key is that var r = new RegExp(str); eval("/" + r.source + "/")
should give you a regexp identical to r.
Implementations don't follow this too closely. IE doesn't handle empty string properly. FF/Chrome don't seem to handle "\n" properly. There are probably other interesting cases to be discovered.
Activity