Regular Expressions
Get a brief introduction to Regular Expressions and their importance in JavaScript.
We'll cover the following...
The previous validations were quite primitive as many strings containing a @
character are not valid email addresses. To perform more advanced checks, you can use a powerful tool: regular expressions.
A regular expression defines a pattern to which strings are compared, searching for matches. Many programming languages support them. What follows is just an introduction to the vast domain of regular expression.
Let’s get started by trying to create a regular expression checking for the presence of an @
character inside a string. Here’s the associated JavaScript code.
A JavaScript regular expression is defined by placing its pattern between a pair of /
characters. It’s an object whose test()
method checks matches between the pattern and the string passed as a parameter. If a match is detected, this method returns true
, and false
otherwise.
The following table presents some of the numerous possibilities offered by regular expressions.
Pattern | Matches if | Match | No Match |
---|