RegExp.escape takes a String and returns it "escaped" for RegExp.
Lets us build a regular expression out of a string without treating special characters from the string as special regular expression tokens.
let needle = input.value; // "Hello.Friend"
let re = new RegExp(needle); // problem `.`
let haystack = re.exec("HellofFriend"); // matches
RegExp.escape("The Quick Brown Fox");
// "The Quick Brown Fox"
RegExp.escape("Buy it. use it. break it. fix it.");
// "Buy it\. use it\. break it\. fix it\."
RegExp.escape("(*.*)");
// "\(\*\.\*\)"
RegExp.escape("。^・ェ・^。")
// "。\^・ェ・\^。"
RegExp.escape("😊 *_* +_+ ... 👍");
// "😊 \*_\* \+_\+ \.\.\. 👍"
RegExp.escape("\d \D (?:)");
// "\\d \\D \(\?\:\)"
Treat a string literally when part of a RegExp.
Full escape sets rationale explained here:
https://github.com/benjamingr/RegExp.escape/blob/master/EscapedChars.md
Proposal rationales explained here:
Escapes everything the SyntaxCharacter proposal does.
Escapes `-` additionally for context sensitive inside-character-class matching.
Escapes hex numeric literals (0-9a-f) at the start of the string in order to avoid hitting matching groups and lookahead/lookbehind control characters.
Less readable output but safer.