Chapter 7. Wrangling regular expressions

 

This chapter covers

  • A refresher on regular expressions
  • Compiling regular expressions
  • Capturing with regular expressions
  • Frequently encountered idioms

Regular expressions are a necessity of modern development. There, we said it.

While many a web developer could go through life happily ignoring regular expressions, there are some problems that need to be solved in JavaScript code that 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. Every JavaScript ninja will have the regular expression as an essential part of his or her toolkit.

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 if an element has a specific class name
  • Extracting the opacity from Internet Explorer’s filter property
  • And more...

Let’s start by looking at an example.

7.1. Why regular expressions rock

7.2. A regular expression refresher

7.3. Compiling Regular Expressions

7.4. Capturing matching segments

7.5. Replacing using functions

7.6. Solving common problems with regular expressions

7.7. Summary

sitemap