Skip to content

Commit

Permalink
Fixed global matching for simple strings in 'Find / Replace' operation.
Browse files Browse the repository at this point in the history
Closes #25.
  • Loading branch information
n1474335 committed Jun 15, 2017
1 parent 3faef2c commit 04aac03
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
16 changes: 16 additions & 0 deletions src/core/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 Down
2 changes: 1 addition & 1 deletion src/core/config/OperationConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -1919,7 +1919,7 @@ const OperationConfig = {
args: []
},
"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,
manualBake: true,
inputType: "string",
Expand Down
12 changes: 7 additions & 5 deletions src/core/operations/StrUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,14 +227,16 @@ const StrUtils = {

if (type === "Regex") {
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);
}

return input.replace(find, replace, 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
// is just a string.
find = new RegExp(Utils.escapeRegex(find), modifiers);

return input.replace(find, replace);
},


Expand Down

0 comments on commit 04aac03

Please sign in to comment.