Status: New
Owner: ----
New issue 3229 by [email protected]: Slashes in regex.source should be
escaped
http://code.google.com/p/v8/issues/detail?id=3229
When a regex is created using the `RegExp` global function (with or without
`new` in front of it), and the pattern string contains a slash, the `source`
property of the constructed regex does not follow the [spec][1]:
15.10.4.1 new RegExp(pattern, flags)
[...] let P be [...] and ToString(pattern) otherwise [...]
[...]
Let S be a String in the form of a Pattern equivalent to P, in which
certain
characters are escaped as described below. [...]
[...]
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. [...]
[...]
The source property of the newly constructed object is set to S.
[...]
15.10.6.4 RegExp.prototype.toString()
Return the String value formed by concatenating the Strings "/", the
String
value of the source property of this RegExp object, and "/"; plus "g" if
the
global property is true, "i" if the ignoreCase property is true, and "m"
if
the multiline property is true.
NOTE The returned String has the form of a RegularExpressionLiteral that
evaluates to another RegExp object with the same behaviour as this object.
[1]: http://es5.github.io/#x15.10.4.1
Contrary to the spec, this is how v8 behaves:
$ node --version && node
v0.10.26
> RegExp("/").source
'/'
> RegExp("/").toString()
'///'
> eval(RegExp("/").toString()) instanceof RegExp
false
This is how Firefox behaves (run in its web console), which is expected:
RegExp("/").source
"\/"
RegExp("/").toString()
"/\//"
eval(RegExp("/").toString()) instanceof RegExp
true
I've run into this problem in [real code][2], where I had to write
"module.exports = /" + regex.source.replace(/\//g, "\\/") + "/g"
instead of simply
"module.exports = " + regex.toString()
[2]:
https://github.com/lydell/js-tokens/blob/1261da82314dd3442b94b937dfb8b71e5cd72dd4/generate-index.js#L16
--
You received this message because this project is configured to send all
issue notifications to this address.
You may adjust your notification preferences at:
https://code.google.com/hosting/settings
--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
---
You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.