Skip to content

Chapter 23 of 33

Regular Expressions

Match and transform text with groups, classes, assertions, flags, and safe patterns.

40 minutes 10 quick checksBy Subha Prasad
Lesson 23 of 33Course navigation

Lesson content

Read, practise, then check your understanding

Regular expressions are concise pattern programs. Literals suit fixed patterns; new RegExp supports runtime construction and requires careful escaping.

Structured matching

const pattern = /^(?<user>[a-z0-9._-]+)@(?<host>[a-z0-9.-]+)$/iu;
const match = pattern.exec(input);
if (match) console.log(match.groups.user, match.groups.host);

Classes, quantifiers, groups, alternation, anchors, and lookarounds express constraints. Flags include global, case-insensitive, multiline, dotAll, Unicode, and sticky behavior. A global regex mutates lastIndex. Escape user-supplied fragments and avoid nested ambiguous quantifiers that enable catastrophic backtracking. Use a parser for complex grammars.

Knowledge check

Answer every question correctly to complete this chapter.

Which statement best describes regular expression?
Which JavaScript term matches this description: A pattern language for searching and transforming text.
Which statement best describes capture group?
Which JavaScript term matches this description: A parenthesized subpattern whose match is retained.
Which statement best describes character class?
Which JavaScript term matches this description: A pattern matching one character from a defined set.
Which statement best describes global flag?
Which JavaScript term matches this description: The g flag enabling repeated matches and stateful lastIndex behavior.
Which statement best describes Unicode flag?
Which JavaScript term matches this description: The u flag enabling Unicode-aware pattern interpretation.

0 of 10 checks passed

Your progress is saved on this device.

Regular Expressions | JavaScript Lesson | Subha Prasad