-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix handling of special replacements patterns in
String#replaceAll
, c…
…lose #900
- Loading branch information
Showing
6 changed files
with
78 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
var toObject = require('../internals/to-object'); | ||
|
||
var floor = Math.floor; | ||
var replace = ''.replace; | ||
var SUBSTITUTION_SYMBOLS = /\$([$&'`]|\d\d?|<[^>]*>)/g; | ||
var SUBSTITUTION_SYMBOLS_NO_NAMED = /\$([$&'`]|\d\d?)/g; | ||
|
||
// https://tc39.github.io/ecma262/#sec-getsubstitution | ||
module.exports = function (matched, str, position, captures, namedCaptures, replacement) { | ||
var tailPos = position + matched.length; | ||
var m = captures.length; | ||
var symbols = SUBSTITUTION_SYMBOLS_NO_NAMED; | ||
if (namedCaptures !== undefined) { | ||
namedCaptures = toObject(namedCaptures); | ||
symbols = SUBSTITUTION_SYMBOLS; | ||
} | ||
return replace.call(replacement, symbols, function (match, ch) { | ||
var capture; | ||
switch (ch.charAt(0)) { | ||
case '$': return '$'; | ||
case '&': return matched; | ||
case '`': return str.slice(0, position); | ||
case "'": return str.slice(tailPos); | ||
case '<': | ||
capture = namedCaptures[ch.slice(1, -1)]; | ||
break; | ||
default: // \d\d? | ||
var n = +ch; | ||
if (n === 0) return match; | ||
if (n > m) { | ||
var f = floor(n / 10); | ||
if (f === 0) return match; | ||
if (f <= m) return captures[f - 1] === undefined ? ch.charAt(1) : captures[f - 1] + ch.charAt(1); | ||
return match; | ||
} | ||
capture = captures[n - 1]; | ||
} | ||
return capture === undefined ? '' : capture; | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters