Regular Expressions in Javascript

In the past weeks I posted several blogs about regular expressions. If you have missed them, you can find them here. They explain regular expressions in detail from the ground up. You can also find a lot of examples there. In the upcoming weeks I will show you how you can use regular expressions in several programming languages and Javascript is the first one. Javascript is your programming language of choice if you want to create websites (some Python users might disagree but I will create a post on regular expressions in Python, too). Regular Expressions are very useful if you want to validate user input in a form on your page. Examples could be mail addresses or zip codes. In this post I will show you how to execute the following tasks in Javascript:

  • test if a string matches a regular expression
  • find all matches in a string
  • find & replace in a string


First I will introduce to you the syntax of regular expressions in Javascript. It has a very nice build-in literal to work with regular expressions. If you remember my introductionary posts about regular expressions you know that I used forward slashes to distinguish RegExes from strings. In a way Javascript does this, too. The following two methods of defining a regular expression object are the same:

var regex1 = new RegExp('[12]34(56)+');
var regex2 = /[12]34(56)+/

As you can see in the first line I defined a new RegExp object that takes the regular expression as a string parameter. In the second line we defined the regular expression using the literal. You can add the following three flags to your regular expression:

  • g: do a global search which means that not only the first but every match will be returned
  • i: do a case-insensitive matching
  • m: do a multi-line matching which means that ^ and $ do not stand for the beginning and the end of the whole string but for the beginning and the end of a single line

You can add the flags to the regular expressions like this:

var regex1 = new RegExp('[12]34(56)+', 'gm');
var regex2 = /[12]34(56)+/gm;


There are several ways how you can test if a string matches your pattern but the easiest one would be calling the method test() on your regular expression object. This method will return true or false based on if there was a match or not. Here is an example:

var regex = /[12]34(56)+/g;
var text = "134562345656";
console.log(regex.test(text)); // test if there is a match

This will print true to the console since there are two matches: 13456 and 2345656. Find more about the test method here.

Find all matches in a string

 A match with additional information like the starting index

A match with additional information like the starting index

 All matches without additional information

All matches without additional information

Now that you know that there is a match in your string you want to know where it is. You can find that out by either calling the method match() on your string or exec() on your regular expression object. The match() method either returns the first match with additional information like the index of the first matched character or all matches in an array (without the additional info). The decisive flag here is the global search flag g. You can test the following code to see the difference (note that you have to call match() on the string and not on the regular expression object).

var regex_global = /([12]34)(56)+/g;
var regex = /([12]34)(56)+/;
var text = "134562345656";

console.log(text.match(regex)); // return first match with info
console.log(text.match(regex_global)); // return all matches (global search)

The exec() method will always return one match and its additional information. So calling exec() for the first time will return the first match of the pattern. Calling it again will return the second and so on. If there is not match left the method will return null. Now you could start again. To get all matches together with the starting index you could write the following method:

var re = /([12]34)(56)+/g;
var text = "134562345656";

var getAllMatches = function(s, re){
    var matches = [];
    var match;
        match = re.exec(s);
                match: match[0],
                index: match.index
    return matches;


The result in the console would look like this:

 Array with all matches and their indices

Array with all matches and their indices

Find and Replace iN a string

Now that you have found every match in a string you may want to manipulate it. You can use the replace() function of the string to do that. You can pass a regular expression object as the first parameter. Let's put nines around the second group in our example. You can reference the groups with the dollar sign ($). This works exactly like I described it in my tutorial on search and replace in Visual Studio Code.

var re = /([12]34)((56)+)/g;
var text = "134562345656";
console.log(text.replace(re, '$19$29'))

The result looks like this: 


As you can see we added nines around group 2. This concludes this post about regular expressions in Javascript. There are plenty of additional resources online to deepen your knowledge. After a short summary I will provide some links to these resources.


To get started with regular expressions in Javascript you need to know the following things:

  1. You can create regular expressions using new RegExp('regular expression as string','flags as string') or the literal notation with surrounding forward slashes: /regular expression/flags
  2. You can check if there is a match by calling test(string) on the RegExp object
  3. String.match(RegExp) or RegExp.exec(String) will return the matches and additional information
  4. Using String.replace(RegExp, ReplaceString) you can search and replace inside a string

Further Reading

Here are the corresponding W3Schools documentations on the methods:

More information on the RegExp object can be found here. is a great testing environment for regular expressions. It is based on Javascript so you should get the same results.

If you have feedback or found an error please comment below or tweet at me. I am happy to update this blog post in the future. In the upcoming weeks I will release more blog posts about regular expressions in other programming languages.

JSFiddle with examples

The following JSFiddle contains the examples that were shown in this post.