Newly availableA strong default whenever user or runtime text needs to become part of a regex pattern safely.

Overview

RegExp.escape() converts a string into a literal-safe fragment that can be inserted into a regular expression. It helps avoid accidental special-character behavior when patterns come from variable input.

Browser support

Feature Desktop Mobile
Chrome
Edge
Firefox
Safari
Chrome Android
Safari iOS
136
136
134
18.2
136
18.2
1+Supported (version) Not supported Has note Sub-feature descriptions sourced from MDN Web Docs (CC BY-SA 2.5)

Syntax

JAVASCRIPT
const userInput = 'price: $10.00 (USD)';
const escaped = RegExp.escape(userInput);
// 'price\\: \\$10\\.00 \\(USD\\)'

const regex = new RegExp(escaped);
regex.test('price: $10.00 (USD)'); // true

Live demo

yu-za-input. safeall search

Inputsymbol positivecharacter and, searchresult hilight..

PreviewFullscreen

escape-presult. confirm

RegExp.escape that. like escape-p row or inspect it..

PreviewFullscreen

riteralmatch. valid-tion

symbol includestring that,. middle to. include or check..

PreviewFullscreen

Use cases

  • Safe dynamic search

    Escape user input before building a regex so characters like . or * are treated literally.

  • Pattern-building utilities

    Combine trusted regex structure with escaped variable text for configurable search tools.

Cautions

  • Escaping makes text literal; do not use it when you intentionally want users to supply raw regex syntax.
  • It helps with pattern construction, but you still need broader performance and validation safeguards for complex searches.

Accessibility

  • Safer pattern construction reduces validation bugs that can lead to confusing error states for users.

Powered by web-features