TOC

The community is working on translating this tutorial into Italian, but it seems that no one has started the translation process for this article yet. If you can help us, then please click "More info".

Regular Expressions:

Introduction

Regular Expressions (often shortened to "regex" or "regexp") is a sequence of characters specifying a search pattern. This allows you to look for strings, either simply to test if they are there, to extract them for use elsewhere, or to perform search/replace operations.

JavaScript has excellent support for regular expressions, but they are not unique to the JavaScript language. In fact, regular expressions was invented many years before JavaScript and they are supported by most programming languages, either natively or through an addon library.

Defining a regular expression

Most programming languages comes with an object, a class or a set of methods for dealing with regular expressions, but in In JavaScript, they are a direct part of the specification - you can write them directly in your code, using a specific notation, or you can use the RegExp object.

But first, let's see how a regular expression might look:

[0-9]+

This is a very simple regular expression, allowing us to look for a number. If we want to use it in JavaScript, we can define it like this, using the JavaScript literal notation for regular expressiosns:

let regex = /[0-9]+/;

Notice the special notation: The actual regular expression has a forward slash before and after it - this allows the JavaScript interpreter to know that we are defining a regular expression. As an alternative, simply create a new object using the RegExp object, like this:

let regex = new RegExp("[0-9]+");

It will accomplish the same - in fact, the literal notation is just syntac sugar, because behind the scenes, it will also create a RegExp object. So, how you do it is really up to you. I often prefer the latter version, because it's so clear what's happening, but you will see many examples using the literal notation across JavaScript code found online, so pay attention to this version as well.

Using the regular expression

Obviously, just creating a RegExp object doesn't get us anywhere - we have to use one of the helpful methods. One of the simplest method is the test() method - it will simply tell you if a match can be made using the regular expression against a string. Here's an example:

let testString = "Hello, I'm 42 years old";
let regex = new RegExp("[0-9]+");
if(regex.test(testString))
	alert("String contains a number!");
else
	alert("String does NOT contain a number!");

Of course, you can do a whole lot more than just that, but let's talk about all the regular expression features of JavaScript in the next articles.

Summary

Using Regular Expressions (regex), you can define a search pattern and then perform search and search/replace operations on a string. Regular expressions are almost like a mini-programming language and can be quite complex and extremely versatile.

The purpose of this tutorial is not to teach you about all the aspects of regular expressions, but instead show you all the stuff you can accomplish with them when using JavaScript. So there will be examples of regular expressions, to illustrate various forms of functionality, but to fully understand regular expressions, I suggest that you find a regex tutorial online or at least a cheat sheet.


This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!