Regular expressions in Javascript: What, how, and why?

As a developer, you know that searching, matching, and aggregating are important to succeed in web development routine, as well as performing input validation. And, what about the value of transferable knowledge? The answer’s here: regular expressions (Regex).

Regex enables you to ease searching/matching, and even searching/replacing. Besides, regular expressions are really helpful in terms of knowledge. For example, Regex written in Javascript can be smoothly relatable to Python ones.

This is just the beginning of why we’ve chosen to talk about Javascript RegExp. We’re going to delve into what Regex are, why you shouldn’t miss them, and how to start using them. Ready?

What are regular expressions?

If you’re just getting started on Regex, we got your back! Let’s do a quick 101-Regex intro to  explain what they are. Regular expressions are a sequence of characters that have the power of matching character combinations in strings so as to enable text searching/matching. It’s important for you to know that those expressions are a small language of their own which are also part of programming languages like Python and Javascript.

We can say that they’re a way to describe and identify patterns in a data string. Why? Well, in Javascript you’ll realize that regex are search patterns from sequences of characters; meaning they are Javascript objects. 

Let’s check out an example: Regex patterns can be used in search engines and editors where filtering/matching texts is key and must be efficiently done. In this case, Regex with the help of a sequence of characters defines search patterns.

Why should you use them?

There are many perks to learning and embracing regular expressions, especially for data management. We’ve chosen our fav-4 benefits to share with you.

1. You’ll want your strings attached: searching/matching of strings

Unlike the known idiom: “No strings attached”, we do know that developers want their strings searched and matched when they need it. Many of them choose Regex for its simplicity. Regular expressions let you perform searching and matching of strings.

If you go with Regex to search your texts, you get true or false if the text is encountered. If matching is what you need, you’ll get an array with the expected text when trying to match it from or to a group of texts.

2. Can someone please say if it’s right or wrong? Input Validation

Taking advantage of regular expressions includes input validation. A lot of developers rely on Regex to make sure users can only use numbers and letters on their passwords and that emails contain values, such as: As you might know, this validation task is key for the developing process and for the proper use of the product you create.

3. Is your extraction mode on? Web scraping

Have you ever dreamed of extracting substrings from strings by just pointing to a webpage and easily getting the data that matches your pattern? This is just one of the tasks that can be performed by using Regex when web scraping. In case you’re wondering, web scraping is about data extraction from websites.

4. You have the data from the website but there’s more

Regex can easily help you aggregate and map data if you need it as fuel for your analytics purposes. Storing all the information from data wrangling for future tasks is also possible. This asset helps you ease the whole retrieving process.

One quick example of what you can do when mapping data is that you can easily examine and arrange data from the website into the format you want so as to make the decision-making process smooth and efficient.

How can you create a Regex?

If you decide to create a regular expression in Javascript, you can both do it with Regex Constructor or by what’s known as RegExp Literal which means that you have to use forward slashes to enclose the pattern.

You have to wisely choose the method that better suits your needs. For example, if you want to build a regex dynamically, RegExp Literal shouldn’t be your choice, you should go with the Regular Expression Constructor instead. There might also be cases where you want to create regular expressions dynamically, in which case RegExp Literal won’t work, so you have to use a regular expression constructor.

Note that, regardless of the method you choose, you’ll always get RegExp objects with the same methods and properties attached. Now let’s discover the two ways you can create a Regex.

Regular Expression Constructor

If you choose this way, pay attention to its syntax: new RegExp(pattern[, flags]). Now, to create a regular expression following this path, you should start by calling the RegExp() constructor function. This would be like this:sz

const reguarExp = new RegExp('abc');

And for instance:

const regex = new RegExp(/^a...s$/);
console.log(regex.test('alias')); // true

Look at the example. In this case, the string alias matches the RegExp pattern  /^a…s$/, and the test() method—there are lots of methods you can use with Javascript RegExp—makes it possible to verify that the string matches the pattern.  

Just in case you were wondering about the metacharacters in the example—^ and $—, note that in Javascript RegExp, metacharacters are used to specify regular expressions. 

Regular Expression Literal

When looking at the syntax of this way of creating a RegExp, it would be: /pattern/flags. In this case, the regular expression is a pattern enclosed between slashes: cost regularExp = /abc/;. /abc/ is a regular expression. 

Do you want to know more about all you can do with Javascript and how to make the most out of it? Jump into our blog and discover this and more about the tech world.