Regex Advanced

A regular expression (regex) is a pattern used to search, match, and replace text, letting you describe complex string rules — like email or phone formats — with a compact, special syntax.

Learn Python, JavaScript, Java and more with free interactive lessons, real projects and AI-powered help. Beginner-friendly.

Part of the free JavaScript course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

Master powerful regex patterns used in production systems for search, validation, parsing, and text processing

What You'll Learn

Regular expressions (regex) are one of the most powerful but misunderstood tools in JavaScript. Basic patterns like /abc/ or \d+ barely scratch the surface. In large-scale applications—search engines, data validators, document parsers, AI-driven text extraction, authentication workflows—advanced regex features determine whether processing is fast, accurate, and maintainable.

While this online editor runs real JavaScript, some advanced examples may have limitations. For the best experience:

Understanding the Regex Engine

JavaScript uses a backtracking engine , which tries different paths until it finds a match or fails. Understanding this behaviour is essential to writing patterns that don't freeze the browser.

Catastrophic Backtracking Example

Lookaheads & Lookbehinds

Lookarounds match conditions without consuming characters , enabling ultra-flexible logic.

Positive Lookahead

Negative Lookahead

Positive Lookbehind

Negative Lookbehind

Named Capture Groups

Named groups make regex readable and maintainable.

Backreference with Named Groups

Unicode & International Text

JavaScript regex with the u flag unlocks global text matching.

Greedy vs Lazy Quantifiers

Greedy (Default)

Lazy (Non-Greedy)

Simulated Possessive (Atomic)

Real-World Pattern: HTML Attributes

Building a Tokenizer

Regex can simulate a tokenizer without a parser.

Dynamic Pattern Generation

Hard-coded patterns don't scale. Build regexes dynamically for large systems.

Performance Optimization

Regex that works for 10 strings may fail catastrophically on 10 million.

Optimization Techniques

Security Patterns

Essential Patterns Every Developer Should Know

What You Learned

Practice quiz

What does a positive lookahead (?=...) do?

  • Consumes the matched characters
  • Matches the start of the string
  • Asserts what follows without consuming characters
  • Repeats the previous group

Answer: Asserts what follows without consuming characters. Lookaheads check a condition ahead without consuming any characters from the match.

What is the result of /user(?=\d+)/.test('user123')?

  • true
  • false
  • user

Answer: true. 'user' is followed by digits, so the positive lookahead succeeds and test returns true.

What does the negative lookahead /user(?!\d)/ match?

  • 'user' only if followed by a digit
  • Any digit after 'user'
  • Nothing ever
  • 'user' only if NOT followed by a digit

Answer: 'user' only if NOT followed by a digit. (?!\d) asserts that 'user' is NOT immediately followed by a digit.

How do you read a named capture group called year from a match m?

  • m.year
  • m.groups.year

Answer: m.groups.year. Named groups are exposed on the match's .groups object, e.g. m.groups.year.

By default, quantifiers like .* in JavaScript regex are:

  • Greedy (match as much as possible)
  • Lazy (match as little as possible)
  • Atomic
  • Possessive

Answer: Greedy (match as much as possible). Quantifiers are greedy by default, expanding to match as much as they can.

What does '<div>Hello</div>'.match(/<.*?>/) return?

  • <div>Hello</div>

The lazy .*? matches as little as possible, stopping at the first >, giving '<div>'.

Which flag enables Unicode property escapes like \p{Letter}?

  • g
  • u
  • i
  • m

Answer: u. The u (unicode) flag is required for \p{...} property escapes to work.

Why does JavaScript's backtracking engine make /(a+)+b/ dangerous?

  • It is a syntax error
  • It only matches one character
  • It ignores the b
  • It can cause catastrophic backtracking on long non-matching input

Answer: It can cause catastrophic backtracking on long non-matching input. Nested quantifiers let the engine try exponentially many groupings, freezing on inputs like many a's and no b.

Before injecting user input into a new RegExp, you should always:

  • Uppercase it
  • Escape regex special characters
  • Reverse it
  • URL-encode it

Answer: Escape regex special characters. Escaping special characters prevents regex injection and unintended pattern behavior.

What does \k<word> reference in a pattern with (?<word>...)?

  • A new capture
  • A lookahead
  • The text previously matched by the named group 'word'
  • The whole match

Answer: The text previously matched by the named group 'word'. \k<word> is a backreference to the text the named group 'word' captured, useful for detecting duplicates.