liveBook · Manning

Regular expression

As explained in Secrets of the JavaScript Ninja, Second Edition

A pattern for matching character sequences in text.

Need to check a string, extract part of it, or change every occurrence of a pattern? A regular expression, or regex, expresses a pattern for matching text. It can turn a long sequence of character-by-character checks into a short statement, though fluency takes practice. section 10.0 section 10.1

Check a ZIP-code-shaped string

`/^\d{5}-\d{4}$/.test(candidate)` checks for five digits, a hyphen, and four digits. The `^` and `$` anchors require the pattern to span the string rather than merely appear inside it. section 10.1 section 10.7

Build and use a pattern in JavaScript:

  • Use a literal such as `/test/i` when the pattern is known while writing the code. Use `new RegExp("test", "i")` when constructing it at runtime; backslashes in constructor strings need an additional escape. section 10.2.1 section 10.3
  • Combine character sets (`[abc]`), repetition (`?`, `+`, `*`), groups (`()`), and alternatives (`|`). Flags include `i` for case-insensitive matching and `g` for all occurrences; `m`, `y`, and `u` provide multiline, sticky, and Unicode-escape behavior. section 10.7
  • Use `test` for a match check, `match` to obtain matched text and captures, or `replace` to change matches. Captured portions can also be referenced within a pattern. section 10.3 section 10.7

A regex is compiled when created, then executed against strings. If a complex pattern will be used repeatedly, create it once and keep the object: browser compilation caches cannot be assumed. This is particularly useful when building a pattern dynamically, such as one for checking element class names. section 10.3

Watch for newline and escape pitfalls

In the JavaScript behavior shown here, `.` does not match a newline: `/.*/` stops before the newline in `"<b>Hello</b>\n<i>world!</i>"`. `/[\S\s]*/` matches the entire string. For a CSS-selector-like identifier, a pattern can accept either word characters or a backslash followed by a character; that alone does not fully support CSS escapes. section 10.6.1 section 10.6.3

Patterns also serve as small parts of browser-facing code: one DOM conversion example matches an opening tag name before choosing the markup needed to create nodes. Keep the pattern focused on the particular matching job, and test it against strings that might expose an overlooked case. section 12.1.1 section 10.0

How Secrets of the JavaScript Ninja, Second Edition covers it

How deep it goes
Explains syntax, flags, creation, compilation, and selected pitfalls through examples, without attempting an exhaustive regex tutorial.
Examples
Uses a ZIP-code-shaped check, newline-containing HTML, escaped CSS-selector identifiers, and element class-name searches.
What it assumes
Assumes familiarity with JavaScript strings and code while introducing regex patterns and creation forms directly.
What you can do after
Apply it via JavaScript regex literals or RegExp to check, capture, and replace text while accounting for compilation and matching pitfalls.
How it connects
Connects regular expressions to JavaScript strings, captures, CSS selectors, element class names, and DOM conversion.

Also explained in

Compare how 27 books explain regular expression →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Secrets of the JavaScript Ninja, Second Edition. The section links point to the parts of the book it draws on.