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.
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.
Note- Different Programming languages implement RegEx in slightly different syntax, Although the RegeEx remains the same. The purpose of this series is to learn and understand RegEx (itself) and not how it is implemented in a particular language. Online Resource to use regex101.com
How does a Regular Expression look like?
The syntax for expressing a RegEx is — /pattern/flags
- Two // — Represents it is a regular expression
- pattern — Represents what match we are looking for.
- flags — (optional) It adds some extra meaning to the pattern. (will be clear in a minute).
Simple Regular Expression — /hello/
This expression simply looks for ‘hello’ in an input. So now if a user enters ‘hello’ there will be a match.
Note-/hello/ expression will only look for the ‘first’ match in the input and also RegEx is case sensitive that means ‘h’ (lowercase) and ‘H’ (uppercase) 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 ‘g’ and ‘i’ flags. (adding extra meaning to the pattern)
- g — stands for the global flag it simply looks for all possible matches.
- i — stands for the case-insensitive flag, it finds matches irrespective of lowercase or uppercase letters.
- So, the expression modifies to — /hello/gi
Now, you can see there are 4 matches.
1. Character Sets
Character sets in RegEx simply help us to find matches from a particular set of characters. Syntax — 
The example above — looks for all matches (g flag )that start with ‘g’ and followed by ‘ot’. Only 1 match found.
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 character set like — [^]
The example above — looks for all matches that start with any character except ‘g’ (^ excludes character g)followed by ‘ot’.
Note — ^ Excludes considers all characters even Numbers (0–9) and Special characters like @,_, etc in it except the ones in the character set. that's why there are 6 matches. Also,
The example above — looks for all matches that start with any character except ‘g or p or d’ followed by ‘ot’. 4 matches found.
Ranges in RegEx help us to find matches within a certain range. Syntax — [start-end].
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.
The example above —/[a-f6–9]ot/g looks for all matches that start with either a character between ‘a and f’ or number between ‘6 and 9’ followed by ‘ot’. 8 matches 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 MetaCharacters, Special Characters, Starting and Ending Characters, etc. Stay Tuned!!. As a Practise try solving this — /[a-zA-Z0–9][^xyz]/g on your own from what you have learned above.