liveBook · Manning

Chapter 10: Chapter 10. Wrangling regular expressions

What you will learn in chapter 10 of Secrets of the JavaScript Ninja, Second Edition

The chapter introduction explains why regular expressions are valuable for concise string processing in JavaScript and identifies common uses.

After this chapter you will be able to

Section by section

Introduction

The chapter introduction explains why regular expressions are valuable for concise string processing in JavaScript and identifies common uses.

Key terms: regular expressions, input validation, CSS selector expression

10.1 Why regular expressions rock

Uses ZIP-code validation to show why regular expressions can express a string-format check more concisely than character-by-character code.

Key terms: regular expressions, ZIP code, decimal digit

10.1 Do you know?

This section poses three self-check questions about regular expressions without explaining their answers.

10.2 A regular expression refresher

This section introduces a brief, nonexhaustive refresher on regular expressions and points readers to books for more detail.

10.2.1 Regular expressions explained

This section defines regular expressions as text-matching patterns and explains how JavaScript creates them and applies flags.

Key terms: regular expression, pattern, terms, operators, regular expression literal, RegExp, constructor, backslash, flags, i

10.2.2 Terms and operators

This section explains how regular-expression terms and operators combine to match literal text, character choices, positions, repetitions, alternatives, and previously captured text.

Key terms: literal, character class operator, set operator, range, escaping, backslash, anchor, ^, $, repetition operators

10.3 Compiling regular expressions

This section explains when JavaScript compiles regular expressions, why reusing a compiled expression can improve performance, and how to construct one from a runtime string.

Key terms: compilation, execution, precompiling, regex literal, regular expression object, new RegExp(), className, whitespace character, double-escape, test method

10.4 Capturing matching segments

This section explains why capturing what a regular expression matched can be more useful than merely checking whether a match exists.

Key terms: regular expressions, capture, matches, pattern

10.4.1 Performing simple captures

Shows how to extract a value embedded in a CSS transform string using a regular-expression capture.

Key terms: transform property, match method, capture, parentheses

10.4.2 Matching using global expressions

This section contrasts the results of local and global `match` calls and shows how repeated `exec` calls retrieve captures across global matches.

Key terms: local regular expression, global regular expression, g flag, match, captures, exec, global search, state

10.4.3 Referencing captures

This section shows how captured text can be reused within a regular-expression match or a replacement string, and notes that ordinary grouping parentheses also create captures.

Key terms: capture, backreference, \1, replace method, replace string, $1, captures, expression grouping, parentheses

10.4.4 Noncapturing groups

This section shows how noncapturing groups preserve regex grouping without producing unwanted captures.

Key terms: noncapturing groups, passive subexpression, capture, (?:...), noncapturing (passive) groups, capturing, expression engine

10.5 Replacing using functions

This section explains how a function passed to String.replace can produce a replacement for each regex match or traverse matches to collect data.

Key terms: replace, replacement value, global search, captures, global regex, capture, camel-cased, string traversal, key-value pairs, hash

10.6 Solving common problems with regular expressions

Introduces the upcoming use of regular expressions to solve common JavaScript problems without explaining any solutions yet.

10.6.1 Matching newlines

Explains why a dot does not match newlines in the JavaScript regular expressions described here and shows two patterns that match across them.

Key terms: period (.) term, newline, character class, alternation regex, passive subexpression, /[\S\s]*/, /(?:.|\s)*/

10.6.2 Matching Unicode

This section shows how to extend a regular-expression character class beyond English ASCII characters and explains the tradeoff of matching a broad Unicode range.

Key terms: character class, \w, U+0080, Basic Multilingual Plane, Unicode punctuation, range

10.6.3 Matching escaped characters

This section explains why escaped characters matter in CSS selector names and describes a limited regular-expression approach to matching them.

Key terms: id values, CSS selector engine, escaped characters, word character sequence, backslash

10.7 Summary

10.8 Exercises

Where this is used later

Concepts explained in this chapter

← Chapter 9: Chapter 9. Dealing with collections Chapter 11: Chapter 11. Code modularization techniques →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.