Latest Code Tutorials

Javascript Regular Expressions Example Tutorial


Get real time updates directly on you device, subscribe now.

Javascript Regular Expressions Example Tutorial is today’s leading topic.  The regular expression is the sequence of characters that form the search pattern. A search pattern can be used for the text search and text 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 let say string. The regular expression can be a single character or a more complicated pattern.

Javascript Regular Expressions Example Tutorial

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.

Let us understand the regExp in Node.js since we can write the javascript on serverside.

#Create a  Node.js project.

Create one project directory using the following command.

mkdir regex

Now, inside the root of the project, create one file called the server.js and add the following code.

// server.js

const str = 'let us browse';

const l ="appdividend");

console.log(l); // 14

Here, what we have done is looking for starting index of the provided string. In our case it is 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. Let us see the following example.

// server.js

const str = 'let us browse';

const l =;

console.log(l); // 14

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

#String replace() With a String

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

// server.js

const str = 'let us browse';

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


Here, you will get the output like let us browse

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

// server.js

const str = 'let us browse';

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


#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 or the end of a word
\uxxxx Find a Unicode character specified by a hexadecimal number xxxx


#Define Regular Expression

In the JavaScript, a regular expression is an object, which can be defined in the following two ways.

The first is by instantiating the new RegExp object using a constructor:

// server.js

const re = new RegExp('appdividend');

console.log(re); // /appdividend/

The second is using a regular expression literal form:

Related Posts
1 of 13
// server.js

const re = /appdividend/;

In the example above, appdividend is called the pattern. In the literal form it’s delimited by the forward slashes, while with the object constructor, it’s not.

#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 a result.

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

// server.js

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.

// server.js

const re = /appdividend/;

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

You will get the output like this.


Javascript Regular Expressions Example Tutorial

#Match Items In Range

Instead of matching the particular string, you can choose to match any character in a range. Let us take an example and understand.

// server.js

const re = /[a-z]/;

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

But the following give you falsely result.

// server.js

const re = /[a-z]/;

console.log(re.test('APPDIVIDEND')); // false

Also, the regular expressions can be combined so that we can make it above example true.

// server.js

const re = /[A-Za-z0-9]/

console.log(re.test('APP10app')); // true

So, the basic of Regular Expression in Javascript is over.

Finally, Javascript Regular Expressions Example Tutorial is over.

Leave A Reply

Your email address will not be published.

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