🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to JavaScript Notes
Topic #91

JS RegExp Objects

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

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

Browser Support

RegExp.escape() is an ECMAScript 2025 feature.

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

Note: See Also: JavaScript RegExp Tutorial JavaScript RegExp Flags JavaScript RegExp Character Classes JavaScript RegExp Meta Characters JavaScript RegExp Assertions JavaScript RegExp Groups JavaScript RegExp Quantifiers JavaScript RegExp Patterns JavaScript RegExp Methods

Want to go beyond the notes?

Join CodingNow 2.0's JavaScript course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

JS RegExp Objects – FAQs

Quick answers about learning JS RegExp Objects in JavaScript.

This free note from CodingNow 2.0 explains JS RegExp Objects in JavaScript — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every JavaScript topic on CodingNow 2.0, including JS RegExp Objects, is 100% free with no signup required.
With focused practice, most students grasp JS RegExp Objects in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now