Demystifying RegEx — 1

Regular Expressions are simply expressions that help us to find matches for a particular pattern within a sequence of characters.

Its most common application is — Form /Input Validation.

For example- While signing up on any website we get small hints like our password should contain at least one number, an alphanumeric character or your email should be a valid email ie. it contains @ in it, etc. All this is possible with the help of RegEx as behind the scenes there is an expression that is constantly looking for matches in your input and only allows you to sign up if you enter a valid input, input that satisfies that pattern.

RegEx is a language in itself and learning it will make you an effective programmer as well as give you an extra notch as many people are not comfortable working with RegEx because of its gibberish syntax.

/^(?=.*[0–9]+.*)(?=.*[a-zA-Z]+.*)[0–9a-zA-Z]{6,}$/

It's okay, I can relate how it feels looking at this RegEx for the first time, but don't worry as the purpose of this series is to take you from a naive to a Regular Expression Wizard.

How does a Regular Expression look like?

The syntax for expressing a RegEx is —

  • — Represents it is a regular expression
  • — Represents what match we are looking for.
  • — (optional) It adds some extra meaning to the pattern. (will be clear in a minute).

Simple Regular Expression — /hello/

This expression simply looks for in an input. So now if a user enters ‘hello’ there will be a match.

/hello/

expression will only look for the ‘match in the input and also RegEx is case sensitive that means ‘h’ () and ‘H’ () letters are treated differently that's why in the above input only 1 match is found.

In order to solve this issue, we can use ‘ and ‘ . (adding extra meaning to the pattern)

  • g — stands for the flag it simply looks for all possible matches.
  • i — stands for the flag, it finds matches irrespective of lowercase or uppercase letters.
  • So, the expression modifies to — /hello/gi
/hello/gi

Now, you can see there are 4 matches.

1. Character Sets

in RegEx simply help us to find matches from a particular set of characters. []

/[g]ot/g

The example above — looks for ()that start with ‘g’ and followed by ‘ot’. Only 1 match found.

/[gphd]ot/g

The example above — looks for all matches that start with either ‘g or p or h or d’ followed by ‘ot’. 4 Matches found.

Excluding Certain Characters — To exclude only certain characters use ‘^’ in your like — [^]

/[^g]ot/g

The example above — looks for all matches that start with any character except ‘g’ ()followed by ‘ot’.

Note — ^ Excludes considers all characters even (0–9) and like @,_, etc in it except the ones in the . that's why there are 6 matches. Also,

/[^gpd]ot/g

The example above — looks for all matches that start with any character except ‘g or p or d’ followed by ‘ot’. 4 matches found.

2. Ranges

in RegEx help us to find matches within a certain range. Syntax — [start-end].

/[a-f]ot/g

The example above — /[a-f]ot/g looks for all matches that start with a character between a and f followed by ‘ot’. 6 matches found.

/[a-f6–9]ot/g

The example above —/[a-f6–9]ot/g looks for all matches that start with either a between ‘a and f’ or betweenfollowed by found

Other examples of ranges can be — A-Z ,0–9, 1–5, etc.

In the upcoming articles, we will be discussing other important concepts of RegEx like , etc. Stay Tuned!!. As a Practise try solving this — /[a-zA-Z0–9][^xyz]/g on your own from what you have learned above.

Web Developer