Chapter 10. Wrangling regular expressions
This chapter covers
- A refresher on regular expressions
- Compiling regular expressions
- Capturing with regular expressions
- Working with frequently encountered idioms
Regular expressions are a necessity of modern development. There, we said it. Although many a web developer could go through life happily ignoring regular expressions, some problems that need to be solved in JavaScript code can’t be addressed elegantly without regular expressions.
Sure, there may be other ways to solve the same problems. But frequently, something that might take a half-screen of code can be distilled down to a single statement with the proper use of regular expressions. All JavaScript ninjas need regular expressions as an essential part of their toolkits.
Regular expressions trivialize the process of tearing apart strings and looking for information. Everywhere you look in mainstream JavaScript libraries, you’ll see the prevalent use of regular expressions for various spot tasks:
- Manipulating strings of HTML nodes
- Locating partial selectors within a CSS selector expression
- Determining whether an element has a specific class name
- Input validation
- And more
Let’s start by looking at an example.