coderef

RegExp.escape()Baseline · Newly available

The RegExp.escape() static method takes a string and replaces any characters that are potentially special characters of a regular expression with equivalent escape sequences. For example, RegExp.escape("[abc]") returns "\\[abc\\]".

Works in the latest version of every major browser, but older devices may not support it yet. Baseline since May 1, 2025.

Browser support

DesktopChrome
1–135Not supported
136+Supportedsince Apr 29, 2025
Current: 155
Edge
12–135Not supported
136+Supportedsince May 1, 2025
Current: 154
Firefox
1–133Not supported
134+Supportedsince Jan 7, 2025
Current: 157
Safari
1–18.1Not supported
18.2+Supportedsince Dec 11, 2024
Current: 27
MobileChrome Android
18–135Not supported
136+Supportedsince Apr 29, 2025
Current: 155
Firefox Android
4–133Not supported
134+Supportedsince Jan 7, 2025
Current: 157
Safari iOS
1–18.1Not supported
18.2+Supportedsince Dec 11, 2024
Current: 27

How to use

Every current browser supports this, but it only became Baseline on May 1, 2025. Use it as a progressive enhancement, or keep a fallback if you support older devices.

Example

Full syntax and examples are on MDN: escape.

Feature detection

JS · check before using
if ("escape" in RegExp || ("prototype" in RegExp && "escape" in RegExp.prototype)) {
  // RegExp.escape is available
}

Documentation (MDN)

Specification