bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/JavaScript/Working with Data
JavaScript•Working with Data

JavaScript RegExp Objects

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind JavaScript RegExp Objects?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

___ pattern = /e/;
3Order

Put the learning moves in the order that makes the concept easiest to apply.

The test() method is a RegExp expression method.
In JavaScript, RegExp is a regular expression object with predefined properties and methods.
The RegExp.escape() Method

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

const pattern = /e/;
pattern.test("The best things in life are free!");

You don't have to put the regular expression in a variable first. The two lines above can be shortened to one:

/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":

/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

// 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, "ExampleSite");

Browser Support

RegExp.escape() is an ECMAScript 2025 feature.

JavaScript 2025 is fully supported in all modern browsers since May 2025 :

Chrome 136Edge 136Firefox 129Safari 18.2Opera 120
Apr 2025Apr 2025Aug 2024Des 2024May 2025

See Also

JavaScript RegExp Flags

JavaScript RegExp Character Classes

JavaScript RegExp Meta Characters

JavaScript RegExp Assertions

JavaScript RegExp Quantifiers

JavaScript RegExp Patterns

JavaScript RegExp Methods

Previous

JavaScript RegExp Patterns

Next

RegExp Methods