Member-only story
Regular expressions, also known as regex or regexp, strike fear into the hearts of many developers. Their cryptic syntax resembles an ancient language that only hardened programmers can decipher.
However, every JavaScript developer should add regex to their toolbelt. Like any powerful tool, mastering regex requires practice but it enables you to easily match, search, and replace text. You can validate forms, parse text, filter outputs, and automate tasks that would be tedious and error-prone to do by hand.
Regex may seem magical but under the hood, it is fundamentally a pattern matching engine. In this article, we will tame regex by building them up step-by-step. Soon, you will be able to read, write, and debug your own regular expressions.
Character Classes
The most basic regex allows you to match specific characters. For example, the regex /abc/ will match the string “abc”.
However, regex has character classes that allow you to match more patterns:
- /.{3}/ matches any string that is 3 characters
- /d/ matches any digit character