JavaScript Regular Expressions

Regular expressions represent a pattern that is used to search for or modify a string. To work with regular expressions, JavaScript defines the RegExp object.

There are two ways to define a regular expression:

const myExp1 = /hello/;
const myExp2 = new RegExp("hello");

The regular expression used here is quite simple: it consists of the single word “hello”. In the first case, the expression is placed between two slashes, and in the second case, the RegExp constructor is used, in which the expression is passed as a string.

RegExp Methods

To determine if a regular expression matches a string, the RegExp object defines the test() method. This method returns true if the string matches the regular expression, and false if it doesn’t.

const initialText = "hello world!";
const exp = /hello/;
const result = exp.test(initialText);
console log(result); // true
 
const initialText2 = "beautiful wheather";
const result2 = exp.test(initialText2);
console log(result2); // false - there is no "hello" in the string "beautifull wheather"

The exec method works similarly – it also checks if the string matches the regular expression, only now this method returns the part of the string that matches the expression. If there are no matches, then null is returned.

let initialText = "hello world!";
let exp = /hello/;
let result = exp.exec(initialText);
console.log(result); // hello

initialText = "beautifull wheather";
result = exp.exec(initialText);
console.log(result); // null

Character groups

A regular expression does not necessarily consist of regular strings, but can also include special elements of regular expression syntax. One such element is a group of characters enclosed in square brackets. For example:

let initialText = "defensive capacity";
let exp = /[Abv]/;
let result = exp.test(initialText);
console.log(result); // true

initialText = "city";
result = exp.test(initialText);
console.log(result); // false

The expression [Abv]specifies that the string must have one of three letters.

If we need to determine the presence of alphabetic characters from a certain range in a string, then we can immediately set this range:

 
let initialText = "defensiveness";
let exp = /[a-z]/;
let result = exp.test(initialText);
console log(result); // true

initialText = "3di0789";
result = exp.test(initialText);
console log(result); // false

In this case, the string must contain at least one character from the range a-z.

If, on the contrary, it is not necessary that the string have only certain characters, then it is necessary to put the ^ sign in square brackets before listing the characters:

let initialText = "defensiveness";
let exp = /[^a-z]/;
let result = exp.test(initialText);
console log(result); // false

initialText = "3di0789";
exp = /[^0-9]/;
result = exp.test(initialText);
console log(result); // true

In the first case, the string should not have only characters from the range a-z, but since the string “defense” consists only of characters from this range, the method test()returns false, that is, the regular expression does not match the string.

In the second case (“3di0789”) the string must not consist only of numeric characters. But since the string also contains letters, the string matches the regular expression, so the test method returns true.

If necessary, we can collect combinations of expressions:

const initialText = "at home";
const exp = /dt]o[nm]/;
const result = exp.test(initialText);
console.log(result); // true

The expression [dt]o[nm] points to those strings that may contain the substrings “house”, “volume”, “don”, “tone”.

Expression flags

Flags allow you to customize the behavior of regular expressions. Each flag represents a single character that is placed at the end of the regular expression. JavaScript uses the following flags:

The global flag allows you to find all substrings that match the regular expression. By default, when searching for substrings, the regular expression selects the first substring found in the string that matches the expression. Although there can be many substrings in a string that also match the expression. To do this, use this flag as a symbol in expressions

The ignoreCase flag allows you to find substrings that match the regular expression, regardless of the case of the characters in the string. To do this, regular expressions use the symbolic

The multiline flag allows you to find substrings that match a regular expression in a multiline text. To do this, regular expressions use the symbol

The dotAll flag allows you to match a dot in a regular expression with any text character, including the line separator. To do this, regular expressions use the symbols

Consider the following example:

const initialText = "hello world";
const exp = /world/;
const result = exp.test(initialText); // false

There is no match between the string and the expression here, since “World” differs from “world” in case. In this case, you need to change the regular expression by adding the property to it ignoreCase:

const exp = /world/i;

Well, we can also use several properties at once:

const exp = /world/ig;

s flag

The s or flag total allows you to match the character. (dot) with any character, including the line separator. For example, take the following example:

const text = "hello\nworld";
const exp = /hello world/;
const result = exp.test(text); // false
console.log(result);    // false

Here in the string “hello\nworld” the words “hello” and “hello” are separated by a line break (for example, we are dealing with multiline text). However, for example, we want JavaScript to ignore line breaks and have the given text match a regular expression /hello world/. In this case, we can use the s flag :

const text = "hello\nworld";
const exp = /hello.world/s;
const result = exp.test(text); // true
console.log(result);    // true

In an expression /hello.world/s, a dot means an arbitrary character. However, without the s flag, this expression will not match multiline text.