Created
November 30, 2022 20:14
-
-
Save keego/b0449a5ee0074f22303761062fa8955a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const syntaxChars = /[\^$\\.*+?()[\]{}|]/g | |
/** | |
* Escapes all special special regex characters in a given string | |
* so that it can be passed to `new RegExp(escaped, ...)` to match all given | |
* characters literally. | |
* | |
* inspired by https://github.com/es-shims/regexp.escape/blob/master/implementation.js | |
* | |
* @param {string} s | |
*/ | |
export function escape(s) { | |
return s.replace(syntaxChars, '\\$&') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment