Open In App

What is RegExp Object in JavaScript ?

Last Updated : 15 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In JavaScript, the RegExp object is a built-in object used to represent regular expressions, which are patterns used to match character combinations in strings. Regular expressions are powerful tools for searching, replacing, and extracting text based on specific patterns.

Syntax:

/pattern/modifiers;

Example: Below is an example of RegExp.

Javascript




// Create a regular expression object using a literal
const pattern = /hello/;
 
// Create a regular expression object
// using the RegExp constructor
const dynamicPattern = new RegExp("world");
 
// Test if a string matches the pattern
console.log(pattern.test("hello world")); // Output: true
 
// Execute a search for a match within a string
console.log(dynamicPattern.exec("hello world"));


Output

true
[ 'world', index: 6, input: 'hello world', groups: undefined ]

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads