HTML Tools

Free RegExp.escape() Helper

Use RegExp.escape(string) to safely escape special regular expression characters in user-provided strings before building dynamic RegExp objects. This static method handles all 14 special regex characters without manual replacement logic. Includes a side-by-side comparison with the old manual escape function approach.

Loading tool...

What is RegExp.escape() Helper?

RegExp.escape(string) is a static method that returns a new string where every special regular expression character is escaped with a backslash. This makes the string safe to use inside a RegExp constructor when building dynamic patterns from user input. The method is part of Baseline 2025 (Chrome 132+) and replaces error-prone manual escape functions that developers have been writing and copying for decades.

quickAnswer

Generate code using RegExp.escape(string) to safely escape special regex characters in user input before building dynamic RegExp patterns. Supports all 14 regex metacharacters. Available in Chrome 132+, Edge, Firefox, Node.js 20+. No more manual escape regex chains.

limitations

  • Not available in environments without Baseline 2025 support. Use a polyfill or the traditional manual replace function for older Node.js versions and legacy browsers.
  • RegExp.escape() only handles string inputs. For RegExp flags or pattern options, you must still build the full pattern string and flags array yourself.
  • The method escapes every special character unconditionally. If you need to build a pattern that mixes literal user input with intentional regex syntax (like wildcards), you must separately handle the intentional syntax.

How to use this tool

  1. Identify where you need to build a RegExp pattern from user-provided or variable input, such as search boxes or filter inputs.
  2. Pass the input string to RegExp.escape() before inserting it into the pattern string for the RegExp constructor.
  3. Use the escaped string in your RegExp pattern: new RegExp(RegExp.escape(userInput), 'gi').
  4. Review the generated code included in the tool output, which shows both the native method and the manual escape function for comparison.

What you can use it for

  • Build a client-side search filter where user keystrokes are turned into a regex for live filtering — escaping prevents invalid or injected patterns from breaking the regex.
  • Create a find-and-replace UI where the user types a literal string to search for and a replacement. Escaping ensures that characters like . or * are treated as literals.
  • Highlight search terms in text content by constructing a RegExp from user input and wrapping matches in a span — escaping guarantees the pattern is syntactically valid.

Use cases

Practical examples

example

Live search filter

A search input on a product listing page filters items by name. The user types something like .NET or (Premium). RegExp.escape() turns each query into a safe pattern so the search works correctly even with special characters.

example

Find and replace in a text editor

A simple browser-based text editor lets users find and replace text. The find string is escaped with RegExp.escape() before building the replacement regex, so searching for $100 or (C) finds literal matches instead of triggering regex syntax.

Common mistakes

  • Forgetting to escape user input and embedding it directly in a regex pattern — this causes syntax errors when the input contains special characters like (, ), or * and breaks the entire regex.
  • Using RegExp.escape() on the entire pattern string that already contains regex metacharacters intentionally — the method escapes everything, so pre-built patterns with quantifiers or groups will lose their special meaning.
  • Assuming RegExp.escape() also handles null or undefined inputs — pass a string value or check the input type before calling to avoid TypeError.

verification

  1. Open Chrome 132+ DevTools Console. Call RegExp.escape('hello.world') and verify it returns 'hello\.world'. Call RegExp.escape('test (1) [2]') and confirm the parentheses and brackets are escaped.
  2. Construct new RegExp(RegExp.escape('.*+?'), 'g') and test it against the literal string '.*+?' — verify it matches, confirming the special characters are treated as literals.

FAQ

Questions about RegExp.escape() Helper

What characters does RegExp.escape() escape?

The method escapes the 14 special regex characters: ^ $ \ . * + ? ( ) [ ] { } |. Each is preceded by a backslash in the returned string. For example, RegExp.escape('hello.world') returns 'hello\.world', and RegExp.escape('price $10') returns 'price \$10'.

How does RegExp.escape() compare to the manual escape function developers used before?

The manual approach typically used string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') — a hard-to-read regex that developers often copied without understanding. RegExp.escape() is a standard API with consistent behavior, no regex-in-a-regex, and no edge-case bugs. It is simpler, faster, and maintained by the browser vendor.

Is RegExp.escape() available in Node.js?

RegExp.escape() is a JavaScript language feature, not a Web API, and is available in Node.js 20+ as a core method on the RegExp constructor. No polyfill or npm package is needed. Check your Node.js version for support.

Can I use RegExp.escape() with Unicode or special spaces?

Yes. RegExp.escape() works on any valid JavaScript string including Unicode characters and unusual whitespace. It only escapes the 14 regex metacharacters and leaves all other characters including non-Latin scripts untouched.

Related tools

More html tools

Html

ARIA Live Region Generator

Generate accessible ARIA live region HTML snippets with role, aria-live, aria-atomic, and aria-relevant attributes. Includes optional JavaScript update function.

Open tool

Html

CSP Hash Generator

Generate CSP hash values for inline scripts and styles. Hash exact code content with SHA-256, SHA-384, or SHA-512 and get the matching CSP directive snippet.

Open tool

Also try

Also try

Seo

AI Crawler robots.txt Builder

Build a robots.txt policy for AI crawlers. Choose from open, selective, or strict presets and block specific AI training bots while allowing search engines.

Open tool