What is Regular Expression(regex) in JavaScript

JavaScript regular expression is the sequence of characters that form the search pattern. A search pattern can be used for the text search and text to replace operations. 

When you search for data in the text, you can use this search pattern to describe what you are searching for in a string. The regular expression can be a single character or a more complicated pattern.

The regular expression, also called regex, is a way to work with strings in a very performant way.

By formulating the regular expression with a special syntax, you can perform the following operations.

  • Search text in a string.
  • Replace substrings in a string.
  • Extract information from a string.

How to create regex in JavaScript

In JavaScript, there are two ways to create a regular expression. The first way is to use a regular expression literal. The script compiles regular expression literals when it loads in the memory. These improve the performance of a program marginally if the expression remains constant.

A regular expression in JavaScript consists of a pattern wrapped with slashes, and the syntax is as follows.

let re = /regular expression/;

The second way is to use the constructor function. You can create a new RegExp object by calling the constructor function of the RegExp object.

let re = new RegExp("regular expression");

Writing a regular expression pattern

To write a regular expression, you start by understanding the special characters used in a regex, such as “.”, “*”, “+”, “?”, and more. Then, you choose a programming language or tool that supports regex, such as Python, Perl, or grep. Finally, you write your pattern using the special characters and literal characters.

A regular expression pattern comprises simple characters, such as /abc/, or a combination of simple and special characters, such as /abc/ or /Chapter (d+).d/. The last example includes parentheses, which are used as a memory device.

Example

const str = 'let us browse appdividend.com';

const l = str.search("appdividend");

console.log(l); // 14

Here, what we have done is look for the starting index of the provided string. In our case, it is an appdividend, so it will return the starting position of the appdividend, which is 14.

The search() method searches the string for a specified value and returns the position of the match.

We can also search the string with a Regular Expression.

const str = 'let us browse appdividend.com';

const l = str.search(/appdividend/);

console.log(l); // 14

In the above case also, we will get the same output.

Regular Expression Patterns

Expression Description
[abc] Find any of the characters between the brackets
[0-9] Find any of the digits between the brackets
(x|y) Find any of the alternatives separated with |

Metacharacters are characters with a special meaning:

Metacharacter Description
\d Find a digit
\s Find a whitespace character
\b Find a match at the beginning of the end of a word
\uxxxx Find a Unicode character specified by a hexadecimal number xxxx

Using the replace() method

JavaScript replace() method replaces the specified value with another value in the string.

const str = 'let us browse appdividend.com';

const replaced = str.replace('appdividend', 'youtube');

console.log(replaced);

Here, you will get the output like let us browse youtube.com.

You can also do the same replace operation using the Regular Expression.

const str = 'let us browse appdividend.com';

const replaced = str.replace(/appdividend/, 'youtube');

console.log(replaced);

Using test() method

You can test a regex using RegExp.test(), which returns the boolean:

test() method is the RegExp expression method.

It searches the string for the pattern and returns true or false, depending on the result.

The following example searches the string for the character “appdividend“:

const re = /appdividend/;

console.log(re.test('you can browse appdividend')); // true

Using exec() method

An exec() method is the RegExp expression method.

It searches the string for a specified pattern and returns a found text.

If no match is found, then it returns null.

const re = /appdividend/;

console.log(re.exec('you can browse appdividend'));

Output

Javascript Regular Expressions Example Tutorial

That’s it.

1 thought on “What is Regular Expression(regex) in JavaScript”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.