---
title: "How to use RegEx in JavaScript for pattern matching?"  
description: "How to use RegEx in JavaScript for pattern matching?"  
author: "Utpal Vishwas"  
published: 2023-10-31  
updated: 2023-11-02  
canonical: https://www.mindstick.com/forum/160352/how-to-use-regex-in-javascript-for-pattern-matching  
category: "javascript"  
tags: ["javascript", "regex", "javascript regex"]  
reading_time: 2 minutes  

---

# How to use RegEx in JavaScript for pattern matching?

How to use [RegEx](https://www.mindstick.com/forum/160354/how-to-find-all-match-strings-using-regex-in-javascript) in [JavaScript](https://www.mindstick.com/articles/874/how-to-create-watermark-text-for-textbox-by-using-javascript) for [pattern matching](https://www.mindstick.com/articles/1865/pattern-matching-in-erlang)?

## Replies

### Reply by Aryan Kumar

In JavaScript, you can use regular expressions (regex) for [pattern](https://www.mindstick.com/forum/2314/what-is-the-mvp-and-mvc-pattern) matching using the **RegExp** object or regular expression literals. Regular expressions allow you to define patterns to search, extract, or manipulate text in strings. Here's how to use regex for pattern matching in JavaScript:

## Using the RegExp Object:

You can create a **RegExp** object to represent a regular expression pattern. Then, you can use its methods like **test()**, **exec()**, and others for pattern matching. Here's an example:

```plaintext
const inputString = "Hello, my email is example@email.com";
const pattern = /[\w.-]+@[\w.-]+\.\w+/; // Regular expression pattern for email matching

const regex = new RegExp(pattern);

if (regex.test(inputString)) {
  console.log("Email found!");
} else {
  console.log("No email found.");
}
```

## Using Regular Expression Literals:

Regular expression literals are defined using forward slashes. You can directly include them in your code without creating a **RegExp** object. Here's the same example using a regular expression literal:

```plaintext
const inputString = "Hello, my email is example@email.com";
const pattern = /[\w.-]+@[\w.-]+\.\w+/; // Regular expression pattern for email matching

if (pattern.test(inputString)) {
  console.log("Email found!");
} else {
  console.log("No email found.");
}
```

## Using the match() Method:

You can use the **match()** method to find all matches of a regular expression pattern in a string. This method returns an array of matches or **null** if no matches are found:

```plaintext
const inputString = "Hello, my email is example@email.com";
const pattern = /[\w.-]+@[\w.-]+\.\w+/g; // Regular expression pattern for global email matching

const matches = inputString.match(pattern);

if (matches) {
  console.log("Emails found:", matches);
} else {
  console.log("No emails found.");
}
```

## Using the replace() Method:

You can use the **replace()** method to find and replace text that matches a regular expression pattern in a string:

```plaintext
const inputString = "Hello, my email is example@email.com. Please contact me at example@example.com";
const pattern = /[\w.-]+@[\w.-]+\.\w+/g; // Regular expression pattern for global email matching
```

Regular expressions in JavaScript provide powerful tools for pattern matching, searching, and manipulating text. You can create complex patterns to match various types of content, such as email addresses, URLs, dates, and more.


---

Original Source: https://www.mindstick.com/forum/160352/how-to-use-regex-in-javascript-for-pattern-matching

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
