liveBook · Manning

Chapter 12: Wrangling regular expressions

What you will learn in chapter 12 of Secrets of the JavaScript Ninja, Third Edition

The chapter introduction explains why regular expressions are useful for searching and parsing strings, while cautioning that some patterns call for a full parser instead.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The chapter introduction explains why regular expressions are useful for searching and parsing strings, while cautioning that some patterns call for a full parser instead.

Key terms: regular expression, pattern, parsing logic, string patterns, full-fledged parser

12.1 Why regular expressions rock

A nine-digit ZIP code example shows how a regular expression can replace a longer loop for checking a string’s format.

Key terms: regular expression, ZIP code, decimal digit

12.2 A regular expression refresher

This introduction places regular expressions in a cross-language context and notes JavaScript’s readable syntax for regex literals.

Key terms: regular expressions, /-delimited syntax, regular expression literals

12.2.1 Regular expressions explained

This section defines regular expressions as text-matching patterns and explains how JavaScript creates them, when to use each creation form, and what regex flags change.

Key terms: regular expression, regex, pattern, terms, operators, regex literal, RegExp constructor, RegExp instance, backslash, dynamically at runtime

12.2.2 Terms and operators

This section explains how common regex terms and operators combine to match characters, positions, repetitions, and captured text.

Key terms: literal, metacharacters, escaping, regex literal, character class, set, range, predefined character classes, anchor, caret

12.3 Compiling regular expressions

Explains how regex compilation differs from execution, why a regex may be reused, and how to choose a matching method and read its result.

Key terms: compilation, execution, regex literal, hoisting, compiled regular expressions, global, sticky, test, exec, match

12.4 Capturing matching segments

The section explains why capturing what a regular expression matched can be more useful than merely checking whether it matched.

Key terms: capture, matches, pattern

12.4.1 Performing simple captures

Explains how capture groups extract parts of a regex match, including how nested, optional, and non-capturing groups affect the returned match array.

Key terms: capture groups, match object, full match, non-capturing group, optional, capture group, undefined

12.4.2 Repeated matching using global expressions

This section shows how to get captures from repeated regex matches with matchAll, interpret the example pattern, and consume results lazily or all at once.

Key terms: local regular expression, global regular expression, g flag, match, matchAll, capturing groups, for…of loop, lazily, array rest syntax, character class

12.4.3 Referencing captures within a regular expression

This section shows how a regex backreference matches an exact repeated word and why word boundaries keep that match from occurring inside longer words.

Key terms: capturing group, backreference, \1, \b, word boundary, anchor

12.5 String replacement with regexes

This section shows how to use a regex and a replacer function with String.replace to transform matched text without changing the original string.

Key terms: replace, regex, immutable, replacer function, match, captures, source string, character class, g flag, capture

12.6 Lookahead, lookbehind, and backtracking

The section explains zero-length regex assertions and introduces lookahead and lookbehind as more general ways to use them.

Key terms: zero-length assertions, ^, $, \b, word boundary, match string, lookahead, lookbehind

12.6.1 Sneaking a peek with lookahead

This section explains how lookaheads test text without consuming it, then uses them to find names before later words and to insert number-grouping commas.

Key terms: lookahead assertion, doesn’t consume, lookahead, anchored, current match position, global regex, .*, \d{3}, $, replace function

12.6.2 Achieving hindsight with lookbehind

This section explains how positive and negative lookbehind test text immediately before a regex match, and why extending that test across all preceding text can be costly.

Key terms: lookbehind, assertion, (?<=\w), current match position, negative lookbehind, (?<!not\s+), implicit anchor, case-insensitive flag, .*, linear search

12.6.3 Backtracking and regex performance

This section explains how regex backtracking works, why some patterns become dangerously slow, and how to reduce that risk.

Key terms: state machine, greedy operator, backtracking, catastrophic backtracking, non-capturing group, zero-length sequence, $ anchor, ReDoS, denial of service, DFA

12.7 Summary

Concepts explained in this chapter

← Chapter 11: Buffers and streams Chapter 13: Modules and bundlers →

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.