JS Foundations #12 — Regular Expressions

Regex syntax, flags, test/exec/match/replace, character classes, quantifiers, anchors, capturing groups, lookahead, and the common patterns used in interview problems.

10 min read
JavaScript
Fundamentals
Regex

TABLE OF CONTENTS

Regular expressions are JavaScript's pattern-matching engine. You'll need them for path parsing, string escaping, input validation, and search-and-replace. This article covers the syntax, flags, methods, and common patterns interviewers expect you to know.


1. Two Ways to Create a Regex

Loading editor...

Use the constructor when the pattern comes from a variable or includes user input (which must be escaped).


2. Flags

FlagNameWhat It Does
gGlobalFind all matches, not just the first
iCase-insensitiveIgnore case
mMultiline^ and $ match line boundaries
sDotAll. matches \n too
uUnicodeEnable Unicode features
yStickyMatch at lastIndex exactly

Loading editor...


3. test() — Returns Boolean

Loading editor...

With the g flag, test() advances lastIndex — a surprising stateful behavior:

Loading editor...

When using g with test, reset lastIndex or avoid g if you just want a boolean.


4. exec() — Returns Match Details

Loading editor...

With the g flag, exec() advances lastIndex — call it repeatedly to get all matches.


5. String Methods That Accept Regex

match(regex) — Find Matches

Loading editor...

matchAll(regex) — All Matches with Details

Loading editor...

replace(regex, replacement) — Replace Matches

Loading editor...

Interview use: Path notation conversion (#16 — [0].0), JSON string escaping (#17).

search(regex) — Index of First Match

Loading editor...

split(regex) — Split by Pattern

Loading editor...

Interview use: Parsing dot/bracket paths (#6), parsing multi-class strings (#24).


6. Character Classes

Loading editor...


7. Quantifiers

Loading editor...


8. Anchors and Boundaries

Loading editor...


9. Groups and Alternation

Loading editor...


10. Lookahead and Lookbehind

Loading editor...


11. Common Patterns

Bracket Notation Parsing (#6, #16)

Loading editor...

JSON String Escaping (#17)

Loading editor...

Email Validation (Basic)

Loading editor...


Quick Reference

PatternMeaning
.Any character except \n (all with s flag)
\dDigit [0-9]
\wWord [a-zA-Z0-9_]
\sWhitespace
\D, \W, \SNegations
[abc]One of a, b, c
[^abc]NOT one of a, b, c
+1 or more
*0 or more
?0 or 1
{n}Exactly n
{n,}n or more
{n,m}n to m
^Start of string/line
$End of string/line
\bWord boundary
(x|y)Alternation
(x)Capturing group
(?:x)Non-capturing group
(?<name>x)Named group
x(?=y)Lookahead
(?<=y)xLookbehind

Interview Tips

  • Use literal syntax for static patterns/pattern/flags is cleaner and more performant.
  • Use constructor for dynamic patternsnew RegExp(variable, 'g').
  • Remember that g flag makes test and exec stateful — either avoid g for simple checks or reset lastIndex.
  • Use matchAll instead of exec loops — cleaner for iterating all matches with groups.

Related Articles


Let's Connect

© 2026 Naveen Karthik // Built with React & MUI