What is RegEx ( regular expression) ? It’s finding a pattern within a text. We commonly use for validation text or search through text. This powerful tools can be difficult to apprehend at the beginning but it will definitely make your life easier.

How to declare?

The literal way is just to set your expression between two forward slashes / /

The RegEx will look for the specific word cat” and because the order matter you won’t find any other word (not “tac” for example). By default the RegEx will only find the first match however the solution exist, you need to add a Flag after your forward slash.

The most common is g that means global and it will match all the pattern found or i for case insensitive.

The first approach is pretty easy, between your slash you put the literal match however to get more complex match you will have to use some special characters.

Meta characters

These meta characters have a different meaning and to include them in your expression you will have to “escape” them by adding a backslash in front of it .

Special Characters

Anchors

Phone Number

All seems abstract, let’s practice to make more sense. If we want to match these phone numbers :

Quantifiers and Brackets

Our solution is redundant and imprecise the \. will get any value. The Brackets and the Quantifiers are other tools that we can use.

Practice makes progress

Regex might be strange at first glance but to get conformable and make them your allies, understanding the syntax and practicing are the keys.

To test and create your own Regex : https://regexr.com/

A cheat sheet : https://fireship.io/lessons/regex-cheat-sheet-js/

Top 15 RegEx : https://digitalfortress.tech/tricks/top-15-commonly-used-regex/

Software Engineering student @Flatiron