liveBook · Manning

Regex operator

As explained in Secrets of the JavaScript Ninja, Third Edition

A regex operator is syntax that modifies the matching behavior of a regular-expression term or group.

A regex operator modifies a term—the character, character class, or group immediately before it. Think of the term as a noun and the operator as an adjective: in `/[az]{5}/`, `[az]` matches either `a` or `z`, and `{5}` requires exactly five repetitions. The pattern matches `aaaaa`, `zzzzz`, or `zaaaz`, but not `azamataz`. section 12.2.2 section 12.7

The common repetition operators are `*` for zero or more repetitions, `+` for one or more, and `?` for an optional term. Use parentheses to group multiple terms when the operator should apply to the group; groups capture their matches by default, or can be made non-capturing with `?:`. section 12.7

Repetition operators are greedy by default: `*` and `+` consume as much as they can, then the regex engine may backtrack if later terms fail. When the number of possible backtracking paths grows exponentially, matching can become catastrophic and block JavaScript’s thread. section 12.7

How Secrets of the JavaScript Ninja, Third Edition covers it

How deep it goes
Defines terms and common repetition operators, then explains greediness, backtracking, and catastrophic backtracking risk.
Examples
Uses `/[az]{5}/` to demonstrate exact repetition and names `*`, `+`, and `?` as common quantifiers.
What you can do after
Apply repetition operators to specify how many times a regex term may match.
How it connects
Connects operators with terms, character classes, groups, captures, and backtracking.

Related concepts

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