escapeRegExp es-toolkit
Escapes characters with special meaning in regular expressions to literal characters.
Usage
escapeRegExp(str)
Use escapeRegExp when you want to safely use a string in a regular expression pattern. It escapes regex special characters like ^, $, \, ., *, +, ?, (, ), [, ], {, }, and | so they match literally.
It's essential when using user input as a regex pattern.
You can also use it for string replacement.
It's useful for handling file paths or URLs.
Parameters
strstringrequired
The string to escape regex special characters.
Returns
Returns a new string with regex special characters escaped.
Source: es-toolkit
Re-exported verbatim from es-toolkit. Implementation, edge cases, and performance behavior are owned upstream. This page mirrors the documentation at the pinned version; the linked source is authoritative.