Bug #3910
closedError using fn:replace() with "q" flag when search string contains "-" character
100%
Description
In the browser, Saxon-JS 1.1.0 crashes with "Uncaught SyntaxError" when fn:replace() is used (at run-time) with the "q" flag, and the replacement string contains the "-" character.
For example, replace('Saxon-JS', '-', '*', 'q')
fails at run-time. (Note that to force this to be evaluated at run-time rather than compile-time, you should supply some args as parameters.)
Related issues
Updated by Debbie Lockett about 6 years ago
- Related to Bug #3790: Saxon-JS documentation app search problems added
Updated by Debbie Lockett about 6 years ago
- Related to Bug #3833: Problems using fn:replace() with special characters in replacement string added
Updated by Debbie Lockett about 6 years ago
- Subject changed from Error using fn:replace() with "q" flag when replacement string contains "-" character to Error using fn:replace() with "q" flag when search string contains "-" character
Bug discovered while working on the documentation app running with Saxon-JS. Using the app search tool to search for the string "Saxon-JS" (or any text containing the character "-") doesn't work properly. The page hits are found, but the text highlighting fails.
Note the fix for bug #3790 to use the "q" flag with the replace function (as used for text highlighting).
Also note that this bug is related to bug #3833 (which also concerned the replace function). But for this bug I only see the problem running in the browser, not when testing Saxon-JS using Nashorn.
Updated by Debbie Lockett about 6 years ago
- Status changed from New to In Progress
JS unit test bug3910 added.
Error messages from different browsers:
-
Safari - SyntaxError: Invalid regular expression: invalid escaped character for unicode pattern
-
Chrome - SyntaxError: Invalid regular expression:
/\-/
: Invalid escape -
Mozilla - SyntaxError: invalid identity escape in regular expression
Updated by Debbie Lockett about 6 years ago
The problematic Saxon-JS code seems to be the following, in regex.js:
var jsFlags = (hasFlag("i") ? "i" : "") + (hasFlag("m") ? "m" : "") + (hasFlag("g") ? "g" : "") + (fullUnicode ? "u" : "");
...
if (hasFlag("q")) {
return new RegExp(regex.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"), jsFlags);
}
Note that fullUnicode
is true
in the browser, but false
otherwise; which I believe explains the difference when testing in Nashorn. (I originally tried adding a new XSLT30 test for the problem, but since I'm only running this test suite with Nashorn, and not in the browser, this wasn't useful.)
There seems to be a problem with the list of characters that we escape. I think we originally got the escape function we use from stackoverflow (https://stackoverflow.com/questions/3561493/is-there-a-regexp-escape-function-in-javascript).
The Mozilla Regexp page (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions) suggests the following function:
function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}
The difference is whether the characters '-' and '/' are included. I think we just need to remove '-', i.e. don't escape this.
Updated by Debbie Lockett about 6 years ago
- Status changed from In Progress to Resolved
- Fix Committed on JS Branch 1.0, Trunk added
Fix committed on 1.x and trunk branches.
Updated by Debbie Lockett about 6 years ago
- Status changed from Resolved to Closed
- % Done changed from 0 to 100
- Fixed in JS Release set to Saxon-JS 1.2.0
Bug fix applied in the Saxon-JS 1.2.0 maintenance release.
Please register to edit this issue
Also available in: Atom PDF Tracking page