Skip to content

Commit 47cf763

Browse files
committed
Merge branch 'bug-findreplace'
2 parents 3faef2c + 04aac03 commit 47cf763

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

src/core/Utils.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,22 @@ const Utils = {
259259
},
260260

261261

262+
/**
263+
* Escape a string containing regex control characters so that it can be safely
264+
* used in a regex without causing unintended behaviours.
265+
*
266+
* @param {string} str
267+
* @returns {string}
268+
*
269+
* @example
270+
* // returns "\[example\]"
271+
* Utils.escapeRegex("[example]");
272+
*/
273+
escapeRegex: function(str) {
274+
return str.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
275+
},
276+
277+
262278
/**
263279
* Expand an alphabet range string into a list of the characters in that range.
264280
*

src/core/config/OperationConfig.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1919,7 +1919,7 @@ const OperationConfig = {
19191919
args: []
19201920
},
19211921
"Find / Replace": {
1922-
description: "Replaces all occurrences of the first string with the second.<br><br>The three match options are only relevant to regex search strings.",
1922+
description: "Replaces all occurrences of the first string with the second.<br><br> Includes support for regular expressions (regex), simple strings and extended strings (which support \\n, \\r, \\t, \\b, \\f and escaped hex bytes using \\x notation, e.g. \\x00 for a null byte).",
19231923
run: StrUtils.runFindReplace,
19241924
manualBake: true,
19251925
inputType: "string",

src/core/operations/StrUtils.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -227,14 +227,16 @@ const StrUtils = {
227227

228228
if (type === "Regex") {
229229
find = new RegExp(find, modifiers);
230-
} else if (type.indexOf("Extended") === 0) {
230+
return input.replace(find, replace);
231+
}
232+
233+
if (type.indexOf("Extended") === 0) {
231234
find = Utils.parseEscapedChars(find);
232235
}
233236

234-
return input.replace(find, replace, modifiers);
235-
// Non-standard addition of flags in the third argument. This will work in Firefox but
236-
// probably nowhere else. The purpose is to allow global matching when the `find` parameter
237-
// is just a string.
237+
find = new RegExp(Utils.escapeRegex(find), modifiers);
238+
239+
return input.replace(find, replace);
238240
},
239241

240242

0 commit comments

Comments
 (0)