Specify exactly how RegExp.source should be escaped
simonp at opera.com
Tue Mar 19 08:05:17 PDT 2013
The spec says about RegExp.source:
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 source property of the newly constructed object is set to S.
Why is the requirement so vague? I would like the spec to state exactly
how source is to be escaped, maybe with an algorithm like:
1. If S is the empty string, let S be "(?:)".
2. Replace all instances of "/" in S with "\/".
3. Replace all instances of literal new lines in S with ???
Currently, I have no idea what to check for when writing test cases for
the .source property when testing e.g. empty string or a slash as P.
More information about the es-discuss