diff --git a/src/lib/utils.js b/src/lib/utils.js index db17ea5..c7a6531 100644 --- a/src/lib/utils.js +++ b/src/lib/utils.js @@ -21,3 +21,23 @@ export function findDeepObject(targetObject, path) { return result; } + +/** + * Matches all the characters needing replacement. + * + * Gathered from right here: https://stackoverflow.com/a/3561711/16048617. Because I don't want to introduce some + * library for that. + * + * @type {RegExp} + */ +const unsafeRegExpCharacters = /[/\-\\^$*+?.()|[\]{}]/g; + +/** + * Escape all the RegExp syntax-related characters in the following value. + * @param {string} value Original value. + * @return {string} Resulting value with all needed characters escaped. + */ +export function escapeRegExp(value) { + unsafeRegExpCharacters.lastIndex = 0; + return value.replace(unsafeRegExpCharacters, "\\$&"); +}