Escape text for regex, shell, SQL, or JSON strings
{ } EncodeEscape special regex characters
$ echo "price is $9.99 (USD)" | escape (input, opts = {}) => {
switch (opts.mode || "regex") {
case "regex":
return input.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
case "shell":
return "'" + input.replace(/'/g, "'\\''") + "'";
case "sql":
return input.replace(/'/g, "''");
case "json-string":
return JSON.stringify(input).slice(1, -1);
default:
return input;
}
}