escapeRegExp
Escapes regular expression metacharacters (. * + ? ^ $ { } ( ) | [ ] \)
in a string so it can be safely embedded in a RegExp pattern.
Use this before building a RegExp from untrusted or dynamic input — without
it, characters like . or ( change the pattern’s meaning instead of being
matched literally.
Available since v3.0.0
Import
Section titled “Import”Signature
Section titled “Signature”Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
str | string | The string to escape |
Returns
Section titled “Returns”string — The escaped string, safe to embed in a RegExp pattern
Examples
Section titled “Examples”Escape metacharacters before building a RegExp
Section titled “Escape metacharacters before building a RegExp”Without escaping, ”.” and ”?” would change the pattern’s meaning.
Safely search for untrusted user input
Section titled “Safely search for untrusted user input”Escaping lets user-provided text be matched literally instead of as a pattern.
