Regular expression 101

Regular expressions allow us to describe a pattern of characters. They are used for matching and extracting values from strings. For example, if we had a string that contained digits (1,2,3) at various positions, a regular expression would allow us to easily retrieve them:

const string = 'some 1 content 2 with 3 digits';string.match(/1|2|3/g); // => ["1", "2", "3"]

A regular expression is written as a pattern delimited by forward slashes, with optional flags following the final forward slash:


The pattern you write can contain both literal and special characters that together inform the regular expression engine of what to look for. The regular expression we're using in our example contains literal ...

Get Clean Code in JavaScript now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.