liveBook · Manning

Regular expression literal

As explained in Secrets of the JavaScript Ninja, Third Edition

JavaScript literal syntax that creates a regular expression using slash delimiters and optional flags.

A regular expression literal creates a pattern for matching strings, using forward slashes as delimiters: /test/. Add flags after the closing slash, as in /test/ig. section 12.2.1 section 12.7

Prefer a literal when the pattern is fixed: backslashes are easier to read here than inside a string, where they must be escaped. Use the RegExp constructor when assembling a pattern dynamically; take care with arbitrary user-provided patterns, which can consume excessive CPU. section 12.2.1

const pattern = /test/;

See the minimal slash-delimited literal syntax. section 12.2.1

How Secrets of the JavaScript Ninja, Third Edition covers it

How deep it goes
Explains delimiters, flags, and escaping tradeoffs, while leaving regex operators and matching behavior to broader treatment.
Examples
Shows /test/ and a dynamically assembled file-extension pattern for .ts or .js.
What you can do after
Build regexes with slash-delimited literals and trailing flags, choosing RegExp construction when runtime input determines the pattern.
How it connects
Connects literals to RegExp construction, regex flags, and string matching.

Also explained in

Compare how 3 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, Third Edition. The section links point to the parts of the book it draws on.