liveBook · Manning

Regular expression literal

As explained in Secrets of the JavaScript Ninja, Second Edition

JavaScript source syntax that specifies a regular-expression pattern between slash delimiters, optionally followed by flags.

A regular expression literal writes a pattern directly in JavaScript source: enclose the pattern in slashes and put any flags after the closing slash. Use one when the expression is known at development time; use the RegExp constructor to build one at runtime. section 10.7

A literal with a flag

const re = /test/i; The i flag makes matching case-insensitive. The same expression can be created with new RegExp("test", "i").

Recognize the literal and its pattern by checking:

  • The slash delimiters, as in /test/. section 10.8
  • Flags after the closing slash, as in /test/g. section 10.8
  • Pattern syntax inside the slashes, such as character sets, quantifiers, groups, and backreferences. section 10.8

How Secrets of the JavaScript Ninja, Second Edition covers it

How deep it goes
Defines slash delimiters and trailing flags, then tests recognition and basic matching; it does not offer an exhaustive regex tutorial.
Examples
Uses /test/, /test/g, /hello/, and patterns with character sets, groups, and backreferences in recognition and matching exercises.
What you can do after
Practice/perform identifying literals, recognizing flags, and predicting matches for basic patterns.
How it connects
Connects literals to the RegExp constructor, flags, character classes, quantifiers, capture groups, and string match and replace methods.

Also explained in

Compare how 2 books explain regular expression literal →

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.