Merge branch 'bug-findreplace'
This commit is contained in:
commit
47cf763b3f
@ -259,6 +259,22 @@ const Utils = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Escape a string containing regex control characters so that it can be safely
|
||||||
|
* used in a regex without causing unintended behaviours.
|
||||||
|
*
|
||||||
|
* @param {string} str
|
||||||
|
* @returns {string}
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
* // returns "\[example\]"
|
||||||
|
* Utils.escapeRegex("[example]");
|
||||||
|
*/
|
||||||
|
escapeRegex: function(str) {
|
||||||
|
return str.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
|
||||||
|
},
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Expand an alphabet range string into a list of the characters in that range.
|
* Expand an alphabet range string into a list of the characters in that range.
|
||||||
*
|
*
|
||||||
|
@ -1919,7 +1919,7 @@ const OperationConfig = {
|
|||||||
args: []
|
args: []
|
||||||
},
|
},
|
||||||
"Find / Replace": {
|
"Find / Replace": {
|
||||||
description: "Replaces all occurrences of the first string with the second.<br><br>The three match options are only relevant to regex search strings.",
|
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).",
|
||||||
run: StrUtils.runFindReplace,
|
run: StrUtils.runFindReplace,
|
||||||
manualBake: true,
|
manualBake: true,
|
||||||
inputType: "string",
|
inputType: "string",
|
||||||
|
@ -227,14 +227,16 @@ const StrUtils = {
|
|||||||
|
|
||||||
if (type === "Regex") {
|
if (type === "Regex") {
|
||||||
find = new RegExp(find, modifiers);
|
find = new RegExp(find, modifiers);
|
||||||
} else if (type.indexOf("Extended") === 0) {
|
return input.replace(find, replace);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (type.indexOf("Extended") === 0) {
|
||||||
find = Utils.parseEscapedChars(find);
|
find = Utils.parseEscapedChars(find);
|
||||||
}
|
}
|
||||||
|
|
||||||
return input.replace(find, replace, modifiers);
|
find = new RegExp(Utils.escapeRegex(find), modifiers);
|
||||||
// Non-standard addition of flags in the third argument. This will work in Firefox but
|
|
||||||
// probably nowhere else. The purpose is to allow global matching when the `find` parameter
|
return input.replace(find, replace);
|
||||||
// is just a string.
|
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user