Chapter 14
Searching with Regular Expressions
In This Chapter
Finding patterns with regular expressions
Writing regular expressions
Using regular expressions in JavaScript
“Creating problems is easy. We do it all the time. Finding solutions, ones that last and produce good results, requires guts and care.”
— Henry Rollins
Regular expressions are a powerful tool within many programming languages that help you find and change text within documents according to patterns within the text. The syntax for regular expressions can be intimidating at first, but once you get the hang of it, there will be nothing you can’t do with text.
Finding It Out with Regular Expressions
Regular expressions are a way to look for patterns or character combinations in strings.
Example uses for regular expressions include
- Checking a user-entered email address to make sure that it’s in the right format
- Finding and replacing all instances of a person’s name in an article
- Locating capitalized words in the middle of sentences throughout a book
- Finding strings of numbers that look like phone numbers inside a document
Here’s what a regular expression looks like:
^((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}$ ...