loading...

JavaScript RegEx

helloklow profile image Kelsey Low Updated on ・3 min read

One of the most powerful tools in JavaScript is the ability to parse, extract, and validate data easily and efficiently using regular expressions (regex). While using regular expressions may seem a bit confusing and daunting at first, it’s a highly valuable and useful skill to master.

Regular Expressions Defined

Regular expressions are a pattern of characters that are used to construct a search. The defined sequence of characters describes what you are looking for. This can be anything from a single character to a complex pattern and may also include special characters. Using regular expressions we can not only search for a defined pattern, we can execute text replacement operations.

In JavaScript, a regular expression literal defines the desired pattern between two forward slashes.

let re = /abc/

This is most commonly how you will see regular expressions declared. The less common way is to use the regex constructor function.

let re = new RegExp(‘abc’)

Regex Modifiers

There are many modifiers that help make regular expressions more succinct. The following three modifiers are highly useful for even the most basic regular expressions.

i: case-insensitive matching

let str = “I am Sam.”
let re = /sam/i
>> Sam

g: global matching

let str = “I am Sam.”
let re = /am/g
>> am, am

m: multiline matching

let str = “\nI am Sam. \nSam I am.”
let re = /I/m
>> I

Regex Brackets

Brackets allow us to specify a range of characters to be included or excluded from our search. This works the same way with both letters and numbers.

Example: let str = “There are 8 planets”

[abc]: find any characters included within the brackets

let re = /[a]/g
>> a, a

[^abc]: find any characters excluded from the brackets

let re = /[^es]/gi
>> T, h, r, a, r, 8, p, l, a, n, t

[0-9]: find any number included within the brackets

let re = /[8]/g
>> 8

[^0-9]: find any non-number within the brackets (numbers are excluded)

let re = /[^8]/g
>> T, h, e, r, e, a, r, e, p, l, a, n, e, t, s

Regex Metacharacters

Next, we will take a look at regex metacharacters. These are characters with special meaning that help to quickly create specific and effective search patterns. Because there are so many metacharacters, we will only explore a few basic examples to get the gist of how they work.

Example: let str = “Feeling 90%”

w: find a word character

let re = /w/g
>> F, e, e, l, i, n, g, 9, 0

W: find a non-word character

let re = /W/g
>> %

d: find a digit

let re = /d/g
>> 9, 0

D: find a non-digit character

let re = /D/g
>> F, e, e, l, i, n, g, %

b: find a match at the beginning or end of a word
B: find a match not at the beginning or end of a word
s: find a whitespace character
S: find a non-whitespace character
r: find a carriage return character
t: find a tab character

Regex Quantifiers

The final aspect of regex that we will look at is quantifiers. Much like metacharacters, quantifiers act as an efficient way to build on our search pattern.

Example: let str = “Hello there user121”

x+: finds any string with at least one x

let re = /e+/g
>> e, e, e, e

x*: finds any string with 0 or more x’s

let re = /er*/g
>> e, er, e, er

x?: finds any string with 0 or one x

let re = /1?/g
>> , , , , , , , , , , , , , , 1, , 1

x{N}: finds any string with N x’s

let re = /d{2}/g
>> 12

Conclusion

Regular expressions are certainly confusing upon first glance. However just like any other programming principle, the more you practice, the more clear using regular expressions will become. Having a sound understanding of the basics of regex is highly valuable for efficiently combing through data and will play a significant role in how you approach this process within your applications.

When faced with a problem that can be solved using regex, it’s worth investing the time and effort to research which regex metacharacters and quantifiers may provide the best solution. The more you familiarize yourself with what’s possible using regex one problem at a time, the more readily it will come as you continue working with regular expressions. While it has the potential to be a tough nut to crack, feeling comfortable with regex is a worthwhile pursuit.

Posted on Apr 15 by:

helloklow profile

Kelsey Low

@helloklow

Coder. Climber. Constantly curious.

Discussion

markdown guide