---
title: "Explain with example the RegExp Object in javascript"  
description: "Explain with example the RegExp Object in javascript"  
author: "Ashutosh Patel"  
published: 2025-02-14  
updated: 2025-02-14  
canonical: https://www.mindstick.com/forum/161163/explain-with-example-the-regexp-object-in-javascript  
category: "javascript"  
tags: ["javascript", "javascript object"]  
reading_time: 3 minutes  

---

# Explain with example the RegExp Object in javascript

Explain with example the [RegExp](https://www.mindstick.com/forum/160351/explain-the-significance-of-metacharacters-in-regexp-in-javascript) Object in javascript

## Replies

### Reply by Amrith Chandran

Regular expression (`RegExp`) is a sequence of characters that forms a search pattern. It is used for pattern matching and string manipulation in JavaScript.

**Creating a** `RegExp` **Object**

There are two ways to create a regular expression are given as below,

- **Using Literal Notation**

```javascript
let pattern = /hello/;
```

- **Using the** `RegExp` **Constructor**

```javascript
let pattern = new RegExp("hello");
```

## Difference:

Constructors allow dynamic pattern creation, while literal notation is simpler and more commonly used.

#### RegExp Methods

there are several RegExp methods to match a pattern in a given variable values,

- `test()` **- Checks if a pattern exists**

Returns `true` or `false` depending on whether the pattern is found or not.

```javascript
let regex = /hello/;
console.log(regex.test("hello world"));  // true
console.log(regex.test("hi there"));     // false
```

- `match()` **- Finds matches in a string**

Returns an array of matches, or return `null` if no matches are found.

```javascript
let str = "hello world, hello JS";
console.log(str.match(/hello/g));  // ["hello", "hello"]
```

- `replace()` **- Replaces matched text**

It replaces a specific pattern match in given values

```javascript
let text = "hello world";
console.log(text.replace(/hello/, "Hi"));
//Output: "Hi world"
```

- `search()` **- Returns the index of the first match**

It matches a pattern in a given value or we can say that it search a specific pattern in the given value and returns the index number if found.

```javascript
let sentence = "JavaScript is awesome!";
console.log(sentence.search(/is/));
//Output: 11 (index)
```

`split()` **- Splits a string using a regex pattern**

```javascript
let data = "apple,banana,orange";
console.log(data.split(/,/));  // ["apple", "banana", "orange"]
```

#### RegExp Flags (Modifiers)

Flags modify the way the regular expression works.

- `g` : Global match (find all matches)
- `i` : Case-insensitive match
- `m` : Multi-line match

## Example:

```javascript
let caseInsensitive = /hello/i;
console.log(caseInsensitive.test("HELLO"));  // true
```

#### RegExp Special Characters & Metacharacters

`\d`: Matches a digit (0-9)\
`\D`: Matches a non-digit\
`\w`: Matches an alphanumeric character (A-Z, a-z, 0-9, _)\
`\W`: Matches a non-word character\
`\s`: Matches whitespace\
`\S`: Matches non-whitespace\
`^`: Matches the start of a string\
`$`: Matches the end of a string\
`.*`: Matches any character(s)

## Example:

```javascript
let numRegex = /\d+/;
console.log("Order #1234".match(numRegex));  // ["1234"]
```

I hope you understand clearly the RegExp object in javascrip.

Thanks.

**Also, read:** [Explain the Date object in javascript.](https://www.mindstick.com/forum/161162/explain-the-date-object-in-javascript)


---

Original Source: https://www.mindstick.com/forum/161163/explain-with-example-the-regexp-object-in-javascript

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
