Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is RegExp in JavaScript?

In this blog post, we're going to learn about Regular Expressions (RegExp) in JavaScript. But before we dive into the world of RegExp, let's understand a bit about what problems RegExp seeks to solve.

Imagine you're building a web application that allows users to create an account. To ensure that the users provide a valid email address, you need a way to validate the email addresses they provide. One approach to do this is by using a pattern that matches valid email addresses. This is where Regular Expressions come in!

What are Regular Expressions?

Regular Expressions, or RegExp for short, are a powerful tool for working with text. RegExp allows us to search, match, and manipulate text based on specific patterns. They can be quite intimidating at first, but once you understand the basics, they can save you a lot of time and effort.

To give you a real-world analogy, think of RegExp as a "text detective" that helps you find specific patterns in a huge book. You give the detective a description of what you're looking for, and the detective will find all the matches in the book.

Now that we have a basic understanding of RegExp, let's dive into some code examples.

Creating Regular Expressions in JavaScript

In JavaScript, RegExp objects can be created in two ways: using a RegExp constructor or using RegExp literal syntax. The literal syntax is more common and easier to read, so we'll be using that in our examples.

Here's how to create a RegExp using the literal syntax:

const regex = /pattern/flags;

In the example above, pattern is the actual pattern you want to match, and flags are optional characters that specify how the search should be performed. We'll discuss more about flags later in this post.

Let's create a RegExp that matches the word "JavaScript":

const regex = /JavaScript/;

And that's it! We've just created our first RegExp. But how do we use it? Let's find out.

Using Regular Expressions in JavaScript

There are several methods available for using RegExp in JavaScript. In this post, we'll cover the most commonly used ones: test(), exec(), match(), search(), replace(), and split().

test()

The test() method is available on RegExp objects and returns a boolean value indicating whether the pattern is found in the given string or not.

Here's an example:

const regex = /JavaScript/;
const str = 'I love JavaScript!';

console.log(regex.test(str)); // Output: true

In the example above, the test() method returns true because the word "JavaScript" is found in the string str.

exec()

The exec() method is also available on RegExp objects and returns an array of information about the first match found in the given string or null if no match is found.

Here's an example:

const regex = /JavaScript/;
const str = 'I love JavaScript!';

const result = regex.exec(str);
console.log(result); // Output: ["JavaScript", index: 7, input: "I love JavaScript!", groups: undefined]

In the example above, the exec() method returns an array with information about the match found in the string str.

match()

The match() method is available on strings and returns an array of all the matches found in the string or null if no matches are found. It takes a RegExp as its argument.

Here's an example:

const regex = /JavaScript/g;
const str = 'I love JavaScript! JavaScript is awesome!';

const result = str.match(regex);
console.log(result); // Output: ["JavaScript", "JavaScript"]

In the example above, the match() method returns an array with all the matches found in the string str. Notice the use of g flag in the RegExp. This is called the global flag, and it tells the RegExp to find all matches in the string, not just the first one.

The search() method is available on strings and returns the index of the first match found in the string or -1 if no match is found. It takes a RegExp as its argument.

Here's an example:

```javascript const regex = /JavaScript/; const str = 'I love JavaScript!';

const result = str.search(regex); console.log(result); // Output: 7 ``