0% completed
Introduction to Regular Expressions
On This Page
A first regular expression
What regular expressions are used for
The limits of regular expressions
How would you check that a piece of text looks like an email address? You could write a loop that finds the @ sign, checks the characters before it, finds a dot after it, and checks the letters at the end. That code would need many lines, and every new rule would add more of them.
A regular expression solves the same problem in a different way. It is a pattern that describes what the text should look like, and JavaScript compares the text with the pattern for you. Regular expressions are often shortened to regex or regexp.
The diagram reads the pattern /^[0-5]{5}$/ one piece at a time. The ^ marks the start of the text, [0-5] means one digit from 0 to 5, {5} means exactly five of them, and $ marks the end of the text. Together they describe text that is exactly five characters long, where every character is a digit from 0 to 5. Notice the three sample strings: "01234" matches, but "12346" does not, because its last character is 6, and "0123" does not, because it has only four characters.
A first regular expression
In JavaScript, a regular expression is an object of the built-in RegExp type. The most common way to create one is to write the pattern between two forward slashes. The program below creates a pattern for email addresses and tests three pieces of text with it.
true false false
The test method returns true when the text matches the pattern and false when it does not. The second address has no @ sign, and the third one contains a space, so both fail. The pattern looks hard to read now, but it is made of a few small pieces, and the rest of this chapter explains each of them. The same email pattern is used throughout the chapter.
What regular expressions are used for
A regular expression can do more than answer yes or no. String methods accept a pattern too, so the same pattern language can find, replace, and split text.
[ '555.123.4567', '555 987 6543' ] 555-123-4567 [ 'red', 'green', 'blue' ]
The first line finds both phone numbers, although they use different separators. The second line replaces every dot with a hyphen. The third line splits a list whose items are separated by either a comma or a semicolon, with or without a space after it. Each of these tasks would need a loop and several conditions without a regular expression.
These four operations cover most uses of regular expressions in real programs:
- Validation: checking that user input, such as an email address or a postal code, has the expected form.
- Search and extraction: finding every phone number, date, or link in a long text.
- Replacement: changing every match of a pattern, for example to put dates into one format.
- Splitting: cutting text into parts at separators that can vary.
The limits of regular expressions
A regular expression checks the form of text, not its meaning. The email pattern above accepts ann..lee@example.com, which has two dots in a row and is not a valid address. It also rejects ann@studio.photography, which is valid, because the pattern allows only two to six letters after the last dot. A pattern that accepts every valid email address and nothing else is very long. Most programs therefore use a short pattern to catch typing mistakes, and then confirm the address another way, for example by sending a message to it.
Regular expressions also become hard to read as they grow, so a long pattern deserves a comment that explains what it accepts. They are a poor choice for text with nested structure, such as HTML or JSON, where a parser such as JSON.parse does the job correctly. Finally, the basic syntax is shared by many programming languages, but the details differ, so a pattern copied from another language should be tested in JavaScript before you rely on it.
Reading Progress
0%
On This Page
A first regular expression
What regular expressions are used for
The limits of regular expressions