JavaScript RegExp Objects
In JavaScript, RegExp is a regular expression object with predefined properties and methods.
The RegExp Object
In JavaScript, RegExp is a regular expression object with predefined properties and methods.
Using test()
The test() method is a RegExp expression method.
It searches a string for a pattern, and returns true or false, depending on the result.
The following example searches a string for the character "e":
Example
Example Code
javascriptconst pattern = /e/;
pattern.test("The best things in life are free!");Example
Example Code
javascript/e/.test("The best things in life are free!");Using exec()
The exec() method is a RegExp expression method.
It searches a string for a specified pattern, and returns the found text as an object.
If no match is found, it returns an empty (null) object.
The following example searches a string for the character "e":
Example
Example Code
javascript/e/.exec("The best things in life are free!");The RegExp.escape() Method
The RegExp.escape() method returns string where 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.
Example
Example Code
javascript// 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.replace(regex, "EA Academy");Browser Support
RegExp.escape() is an ECMAScript 2025 feature.
JavaScript 2025 is fully supported in all modern browsers since May 2025:
| Chrome 136 | Edge 136 | Firefox 129 | Safari 18.2 | Opera 120 |
| Apr 2025 | Apr 2025 | Aug 2024 | Des 2024 | May 2025 |
Interactive Code Examples
JS Example 1
javascriptconst pattern = /e/;
pattern.test("The best things in life are free!");Example 2
javascript/e/.test("The best things in life are free!");JS Example 3
javascript/e/.exec("The best things in life are free!");JS Example 4
javascript// 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.replace(regex, "EA Academy");