1.
Regular expressions
Regular expressions are used to find strings
certain words in a matching pattern.
Example: If we want to find the The dog chased the cat
word in the string the
, we can use the following regular expression:/the/gi
We can divide this regular expression into several segments:
/
It's the head of this regular expression.
the
Is the pattern we want to match.
/
is the tail of this regular expression.
g
Represents global
(global), which means that all matches are returned, not just the first one.
i
It means ignoring the case, meaning that when we look for a matching string, we ignore the case of the letter.
2.
We can use special selectors in regular expressions to select special types of values.
One of the special selectors is the number selector \d
, which means the number that is used to get a string.
In JavaScript, a digital selector resembles: /\d/g
.
Add a plus sign () after the selector +
, for example: /\d+/g
It allows the regular expression to match one or more numbers.
The tail g
is shorthand for ' global ', which means allowing the regular expression to find all matches instead of just finding the first match.
3.
We can also use the regular expression selector \s
to select whitespace in a string.
Whitespace characters are " "
(whitespace), \r
(carriage return), \n
(line break), \t
(tab), and \f
(page break).
A blank regular expression resembles the following:
/\s+/g
4.
You can use the uppercase version of the regular expression selector to convert any match.
For example: \s
match any whitespace character and \S
match any non-whitespace character.
JavaScript Regular Expressions