escapeRegExp
Escapes the 12 ECMAScript regex metacharacters (. * + ? ^ $ { } ( ) | [ ] \) so the string can be safely interpolated into a RegExp. Prevents regex injection when user input is used in a pattern.
Try it
Section titled “Try it”Import
Section titled “Import”import { escapeRegExp } from "1o1-utils";import { escapeRegExp } from "1o1-utils/escape-reg-exp";Signature
Section titled “Signature”function escapeRegExp({ str }: EscapeRegExpParams): stringParameters
Section titled “Parameters”| Name | Type | Required | Description |
|---|---|---|---|
| str | string | Yes | The string to escape |
Returns
Section titled “Returns”string
Examples
Section titled “Examples”escapeRegExp({ str: "[hello](world)" });// => "\\[hello\\]\\(world\\)"
const re = new RegExp(escapeRegExp({ str: userInput }), "i");re.test(text);Edge Cases
Section titled “Edge Cases”- Throws if
stris not a string. - Returns
""for empty string. - Strings with no special characters are returned unchanged.
- Unicode characters are preserved.
Also known as
Section titled “Also known as”escape regex, regex injection, escape pattern, safe regex
Prompt suggestion
Section titled “Prompt suggestion”I'm using 1o1-utils (npm: https://www.npmjs.com/package/1o1-utils, GitHub: https://github.com/pedrotroccoli/1o1-utils, LLM context: https://pedrotroccoli.github.io/1o1-utils/llms.txt). Show me how to use escapeRegExp to safely build a RegExp from user input