A regular expression (RegEx) is a subset of some programming languages that helps to find a pattern match in data. Practical applications of regex are password, phone number, and email validation.
Creating a Regular Expression
There are two main methods of creating a regular expression:
RegEx constructor
Declare a RegExp pattern using two forward slashes followed by an optional flag.
Before we take a deep dive at both methods, let's take a look at regular expression syntax
let x = /pattern/modifier
Pattern
Patterns are text or any other form of pattern that we are looking to match in an existing data. e.g if we are trying to match the word publish
in an article we find online.
Modifiers Modifiers are optional parameters in a regular expression that determine the type of pattern matching we are trying to perform. Some of the modifiers we have are
g - This is the global modifier which means looking for a pattern in the whole text
i - Case insensitive flag (it matches both lowercase and uppercase patterns to the text)
m - Multiline.
Creating a RegEx with RegEx constructor
We can declare a regular expression without a modifier as seen below.
let pattern = 'car'
let regEx = new RegExp(pattern)
We can also declare a regular expression with a modifier as seen below.
let pattern = 'car'
let modifier = 'ig'
let regEx = new RegExp(pattern, modifier)
We can also pass the pattern and modifier directly into the RegEx object. The pattern is the first argument while the modifier is the second as seen below.
let regEx = new RegExp('car', 'ig')
Creating a RegEx without the constructor
We can also create a regEx using double forward slashes, single or double quotes, and backticks.
let regEx= /car/ig
The code above is the same as writing the code below
let regEx = new RegExp('car', 'ig')
Regular expression methods
There are various RegEx methods and we would be examining them below.
search()
: Tests for a match in a string. It returns the index of the match, or -1 if the search fails.
const data = 'I love JavaScript'
const pattern = /love/g
const output = data.search(pattern)
console.log(output) // logs 2 to the console.
test()
: Tests for a match in a string. It returns a boolean(true or false).
const data = 'I love JavaScript'
const pattern = /love/
const output = pattern.test(data)
console.log(output) // logs true to the console because love exists in the data.
match()
: The match method works in two ways depending on if the global modifier is passed, it returns an array of pattern, index, input, and group if the global modifier is not passed. It returns an array containing the match only if the global flag is passed.
const data = '100 days of code'
const pattern = /days/
const output = data.match(pattern)
console.log(output)
// [ 'days', index: 4, input: '100 days of code', groups: undefined ]
const data = '100 days of code'
const pattern = /days/g
const output = data.match(pattern)
console.log(output) // ['days']
replace()
: Executes a search for a match in a string, and replaces the matched substring with a replacement substring.
In the example below, gasoline replaces electric
but not Electric
because the case insensitivity modifier was not passed
const words = 'Electric cars are generally good for the environment, but electric cars have low acceptability'
const replaced = words.replace(/electric/, 'gasoline')
console.log(replaced)
// Electric cars are generally good for the environment, but gasoline cars have low acceptability
In the sample code below, the ig
modifiers combine to make sure that all occurrence of electric
is replaced with gasoline
const words = 'Electric cars are generally good for the environment, but electric cars have low acceptability'
const replaced = words.replace(/electric/ig, 'gasoline')
console.log(replaced)
// gasoline cars are generally good for the environment, but gasoline cars have low acceptability
const words = '100-days-of-code'
const replaced = words.replace(/-/g, ' ')
console.log(replaced)
// 100 days of code
SPECIAL CHARACTERS IN REGEX
[]
: Used to match a set of characters.
[a-c]
means, a or b or c[a-z]
means, any letter a to z[A-Z]
means, any character A to Z[0-3]
means, 0 or 1 or 2 or 3[0-9]
means any number 0 to 9[A-Za-z0-9]
any character which is a to z, A to Z, 0 to 9
\
: uses to escape special characters
\d
a match where the string contains digits (numbers from 0-9)\D
: a match where the string does not contain digits
.
: any character except new line character(\n)
^
: starts with
eg '^cat', a sentence which starts with the word cat
eg '[^abc] means not a, not b, not c.
$
: ends with
- eg 'love$', sentence ends with a word love
{2}
: Exactly 2 characters
{5,}
: At least 5 characters
{2,7}
: 2 to 7 characters
Now let's practice some of the special characters using examples.
Using square bracket to include both uppercase and lowercase
const pattern = /[Ee]lectric/g // this square bracket means either E or e
const str = 'Electric cars are generally good for the environment, but electric cars have low acceptability'
const matches = str.match(pattern)
console.log(matches)
// [ 'Electric', 'electric' ]
Match two different words, both uppercase and lowercase
let pattern = /[Ee]lectric | [Gg]asoline/g //this square bracket means either E or e, G or g
let str = 'Electric cars are generally good for the environment, but electric cars have low acceptability as compared to gasoline. Gasoline also affects the environment
let matches = str.match(pattern)
console.log(matches)
// [ 'Electric ', 'electric ', ' gasoline', ' Gasoline' ]
Starts with
const str = 'Electric and gasoline cars are fast'
const pattern = /^Electric/g // ^ means starts with
const matches = str.match(pattern)
console.log(matches)
// ['Electric']
Negation or Opposite
^
in square bracket means negation
const str = 'His Electric cars were delivered in January 2020.'
const pattern = /[^A-Za-z,. ]+/g // ^ not A to Z, not a to z, no space, no comma no period
const matches = str.match(pattern)
console.log(matches) //['2020']
Escape Character
const pattern = /\d/g // d is a special character which means digits
const txt = 'This course was created 0n August 12, 2022.'
const matches = txt.match(pattern)
console.log(matches) // [ '0', '1', '2', '2', '0', '2', '2']
Adding the +
character means one or more characters should be added as seen in the sample code below
const pattern = /\d+/g // d is a special character which means digits
const txt = 'This course was created 0n August 12, 2022.'
const matches = txt.match(pattern)
console.log(matches) // [ '0', '12', '2022' ]
In conclusion, regular expressions are worth understanding due to their enormous capabilities and application in different programming languages. It allows you to create patterns that help match, locate, and manage text. Thanks for reading.
Consider following me on social media.