Regular expressions (called Regex or RegExp) are patterns that we can use to match character combinations in strings. These are used in many languages for text processing and manipulations.
Today, we will learn:
In this course, you’ll learn everything about regular expressions, from RegExp Objects, parsing data, and more.
Regular expressions are specially formatted text strings for finding patterns in text. They are commonly used for text processing and manipulation. A regular expression is formed by a sequence of characters to create a search pattern, which can be applied to text search and text replace operations.
A regular expression can be anything from a single character to a complicated pattern.
Regular expressions have many uses. For example, they allow you to check a string of characters for patterns, like in an e-mail address or password. This allows you to see if they match a pattern defined by that regular expression.
Generally speaking, there are two types of regular expressions with one main difference.
\character) to be recognized
Let’s look at the different components in a regular expression. The image below shows the main parts of a RegExp.
Let’s break down the main things you need to know.
/of your regular expression, you can add a number of flags. These will affect the behavior of the RegExp engine parsing your expression.
There are all sorts of special characters that we use to create Regex. We will discuss these later on.
The RegExp object represents an instance of a regular expression.
With the literal syntax, we can create regular expressions directly using their classical notation. However, there are limitations here. One big difference between these approaches is that the object’s constructor allows for a quoted expression to be passed. This allows us to make dynamic expressions.
The literal syntax uses forward slashes (
/pattern/). The constructor syntax uses quotes (
Check out the code below, and see on first line that we’re only able to create a constant expression. However, we can use the object to take advantage of string concatenation, creating a dynamic expression.
let greeting = /[hH]ello/ let prefix = "hH" let suffix = "" let objGreeting = new RegExp("[" + prefix + "]ello" + suffix) console.log(objGreeting) //------------------------------- let prefix1 = "bB" let suffix1 = "w" let objGreeting1 = new RegExp("[" + prefix1 + "]ello" + suffix1) console.log(objGreeting1)
new RegExp(pattern[, flags])
var regexConst = new RegExp('abc');
var regexLiteral = /abc/;
With both methods, the result is a Regex object. They will have same methods and properties. Let’s look at another example that uses both methods.
let myRegExp = /[2b|^2b]/ let myOtherRegExp = new RegExp('[2b|^2b]') console.log(myRegExp) console.log(myOtherRegExp)
There are two methods for testing regular expressions.
RegExp.prototype.test(): to test if a match has been found or not. It accepts a string that we test against a regular expression. It will return true or false if the match is found.
RegExp.prototype.exec(): Returns an array with all matched groups. It accepts a string that we test against a regular expression.
A Regex pattern is composed of simple characters or a combination of simple and special characters. The most basic Regex pattern will simply match the text with the test string.
var regex = /hello/; console.log(regex.test('hello world'));
Simple patterns are constructed of the characters that you want to directly match. For example, the pattern
/cba/ matches character combinations only where the exact sequence “cba” is located.
We can make our expressions more powerful or complex with special characters, like we discussed before. We can use special characters and symbols that you have to memorize and implement in your own code. A few special characters are:
Regular expressions offer five optional flags or modifiers. The two most popular are
g, for a global search, and
i, for a case-insensitive search.
The basic syntax looks as follows:
new RegExp('pattern', 'flags')
Let’s see an example using the constructor syntax.
var regexGlobal = new RegExp('abc','g') console.log(regexGlobal.test('abc abc')); var regexInsensitive = new RegExp('abc','i') console.log(regexInsensitive.test('Abc'));
Special characters are characters with extra meaning. These are collections of characters with preset behaviors that you have to memorize.
Character classes are everything you put inside brackets to let the parser know which characters you want to match. For example,
/[abc]/ would match the first
a inside the string:
Let’s take a look at a few examples of character classes:
Quantifiers are symbols with a special meaning in a regular expression. For example,
+ matches the preceding expression 1 or more times, and
* matches the preceding expression 0 or more times.
There aren’t that many quantifiers, but they allow you to create some complex patterns. Quantifiers can be placed next to a single character, a character class, or a capturing group. They will also affect the way your regular expression is interpreted based on that.
Join a community of more than 1 million readers. A free, bi-monthly email with a roundup of Educative's top articles and coding tips.