← Back to release summary

RegExp.escape

Category
JavaScript
Type
Chromium catches up
Status
In developer trial (Behind a flag) (Chrome 136)
Intent stage
None

Summary

RegExp.escape is a static method that takes a string and returns an escaped version that may be used as a pattern inside a regular expression. For example, copied from the proposal explainer: ``` const str = prompt("Please enter a string"); const escaped = RegExp.escape(str); const re = new RegExp(escaped, 'g'); // handles reg exp special tokens with the replacement. console.log(ourLongText.replace(re)); ```

Motivation

To provide a standard method to escape strings for use inside regular expressions, which is a common use pattern and is error-prone due to a large number of corner cases.

Standards & signals

View on chromestatus.com