RegExp escape()
Example
Create a regular expression that matches the string "[*]":
// Escape a text for to use as a regular expression
const safe = RegExp.escape("[*]");
// Build a new reglar expression
const regex = new RegExp(safe);
// Text to replace within
const oldText = "[*] is a web school.";
// Perform the replace
const newText = oldText.match(regex, "W3Schools");
Try it Yourself »
Description
The RegExp.escape()
method returns string where all characters that belongs
to the regular expression syntax are escaped.
This makes it possible to treat characters like +, *, ?, ^, $, (, ), [, ], {, }, |, and \ literally, and not as part of a regular expression.
Syntax
RegExp.escape(string)
Parameters
Parameter | Description |
---|---|
string | Required. The string to be searched |
Return Value
Type | Description |
---|---|
String | An string where regular expression characters are escaped |
Browser Support
RegExp.escape()
is supported all in modern browsers since May 2025:
Chrome 136 | Edge 136 | Firefox 134 | Safari 18.2 | Opera 121 |
Apr 2025 | May 2025 | Jan 2025 | Des 2024 | Jun 2025 |
Regular Expression Methods
Regular Expression Search and Replace can be done with different methods.
These are the most common:
String Methods
Method | Description |
---|---|
match(regexp) | Returns an Array of results |
matchAll(regexp) | Returns an Iterator of results |
replace(regexp, s) | Returns a new String |
replaceAll(regexp, s) | Returns a new String |
search(regexp) | Returns the index of the first match |
split(regexp) | Returns an Array of results |
regexp Methods
Method | Description |
---|---|
regexp.exec() | Returns an Iterator of results |
regexp.test() | Returns true or false |