Closed enexusde closed 8 years ago
By the way, the target of the replace-function is random but the value is \\$&
i guess it is this line:
// Compile to AST and then to compiled function
tmplFn(tmplOrMarkup.replace(rEscapeQuotes, "\\$&"), tmpl);
I think it is a bug because
https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/RegExp/lastMatch
sais its non-standard to use $&
The MDN article you link to is for the non-standard RegEx.lastMatch()
API, which is a programmatic way of accessing the last match. But JsRender is not using that API.
In fact JsRender is using "$&"
as a replacement pattern in a replacement string, in the someString.replace(regex, replacementstring)
call - which is standard usage in JavaScript RegEx scenarios.
See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace, where it says:
The replacement string can include the following special replacement patterns: ... $& Inserts the matched substring.
The regex in that line of code is rEscapeQuotes, which is declared as rEscapeQuotes = /['"\\]/g
- and is a valid JavaScript regex.
That line is looking for '
"
or\
and replacing them with the same character preceded by \
.
Incidentally you should not be trying to run the same regex expressions used in JsRender as if they were Java regex expressions, since in Java the rules and validity of regex expressions can be quite different. JsRender of course runs in a JavaScript environment - the browser, or Node.js on the server...
Closing this, since it does not appear to me to be a JsRender bug.
For your information, i created a bug-report at https://bugs.openjdk.java.net/browse/JDK-8162831 for supporting $& in regex.
Use canoo webtest
Results in