Skip to content

Commit ad1c379

Browse files
committed
fix: handle RegExp patterns in count parameter
The count parameter was calling escapeRegExp() on RegExp objects, which caused a crash. Now properly checks if match is already a RegExp before escaping. Also fixes lint issues (let -> var, quotes, semicolons).
1 parent 845ead4 commit ad1c379

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

index.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,12 @@ module.exports = function reactStringReplace(source, match, fn, count = null) {
9595

9696
return flatten(
9797
source.map(function (x) {
98-
let ret;
98+
var ret;
9999
if (isString(x)) {
100100
if (Number.isInteger(count) && count > 0) {
101101
ret = replaceString(x, match, fn, count);
102-
count -= (
103-
x.match(new RegExp("(" + escapeRegExp(match) + ")", "gi")) || []
104-
).length
102+
var re = isRegExp(match) ? match : new RegExp('(' + escapeRegExp(match) + ')', 'gi');
103+
count -= (x.match(re) || []).length;
105104
} else {
106105
ret = replaceString(x, match, fn, count);
107106
}
@@ -111,4 +110,4 @@ module.exports = function reactStringReplace(source, match, fn, count = null) {
111110
return ret;
112111
}),
113112
);
114-
}
113+
};

0 commit comments

Comments
 (0)