liveBook · Manning

Regex term

As explained in Secrets of the JavaScript Ninja, Third Edition

A regex term is a literal, character class, group, or other pattern unit that can be matched by a regular expression.

A term is a matchable unit in a regular expression: a literal character, a character class, or a group. Terms placed in sequence must match in that order; operators such as {5} can qualify a term. section 12.2.2 section 12.7

Literals and character classes

In /test/, each character is a term, so the pattern matches t followed by e, s, and t. In /[az]{5}/, [az] is one term that matches either a or z; {5} requires five repetitions. section 12.2.2 section 12.2.2

Metacharacters have special meaning. Escape one with a backslash to match it literally; for example, /\\/ matches a backslash. Regex literals avoid the extra escaping required when writing the same pattern as a JavaScript string for RegExp. section 12.2.2

Table 12.1 Predefined character classes and character terms
Predefined term
Matches
.
Any character, except for line break characters unless the s flag is set
\d
Any decimal digit; equivalent to [0-9]
\D
Any character but a decimal digit; equivalent to [^0-9]
\w
Any alphanumeric character including underscore; equivalent to [A-Za-z0-9_]
\W
Any character but alphanumeric and underscore characters; equivalent to [^A-Za-z0-9_]
\s
Any whitespace character (space, tab, form feed, and so on)
\S
Any character but a whitespace character
\b
A word boundary
\B
Not a word boundary (inside a word)

Use the table to compare predefined character terms, including digit, word, whitespace, and boundary forms. section 12.2.2

How Secrets of the JavaScript Ninja, Third Edition covers it

How deep it goes
Explains literal terms, classes, groups, sequential matching, and escaping through focused examples.
Examples
Uses /test/, /[az]{5}/, and an escaped backslash to illustrate literals, classes, and metacharacters.
What you can do after
Understand how to combine literal characters and character classes into sequential patterns and escape metacharacters.
How it connects
Connects terms to regular-expression operators, character classes, groups, and string matching.

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.