# Advanced Regular Expressions

> What are advanced RegExp features in JavaScript? Explain named capture groups, lookaheads, and flags.

Canonical: https://www.internhack.in/learn/interview/javascript-interview/js-regex-advanced
Last updated: 2026-11-28

Difficulty: Intermediate

## Question

What are advanced RegExp features in JavaScript? Explain named capture groups, lookaheads, and flags.

## Answer

Modern JavaScript RegExp supports:

Named capture groups: (?<name>pattern) - access matched groups by name

Lookahead/Lookbehind: zero-width assertions that match position without consuming characters:

- Positive lookahead: (?=...) - match only if followed by...
- Negative lookahead: (?!...) - match only if NOT followed by...
- Positive lookbehind: (?<=...) - match only if preceded by...
- Negative lookbehind: (?<!...) - match only if NOT preceded by...

Key flags: g (global), i (case-insensitive), m (multiline), s (dotAll), u (Unicode), d (indices)

Non-greedy: \*? and +? match as little as possible

**Watch a regex scan the string. Edit the pattern, text and flags under Tweak.**

1. **Scan from each index**: The engine tries to start a match at 0, 1, 2…
2. **Capture groups**: (\\d{3}) and (\\d{4}) record parts of the match.
3. **g flag**: Keeps scanning after a match; without it, the search stops at the first.
4. **match vs exec**: match with g returns all matches without groups; exec/matchAll give groups and index.

## Code examples

### Named Groups and Lookaheads

```
const dateRegex = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/;
const match = '2024-03-15'.match(dateRegex);
console.log(match.groups.year);
console.log(match.groups.month);
console.log(match.groups.day);

const { groups: { year, month, day } } = match;
console.log(`${day}/${month}/${year}`);

const reordered = '2024-03-15'.replace(
  /(?<y>\d{4})-(?<m>\d{2})-(?<d>\d{2})/,
  '$<d>/$<m>/$<y>'
);
console.log(reordered);

const priceRegex = /\d+(?=€)/;
console.log('100€ and 200$'.match(priceRegex));

const dollarRegex = /(?<=\$)\d+/;
console.log('$42 and £100'.match(dollarRegex));
```

Named groups make regex more readable. Lookaheads assert context without including it in the match.

### Flags and Non-Greedy Matching

```
const html = '<div>Hello</div><span>World</span>';
console.log(html.match(/<.+>/)[0]);
console.log(html.match(/<.+?>/)[0]);

const emailRegex = /([\w.-]+@[\w.-]+\.\w+)/g;
const text = 'Contact alice@ex.com or bob@test.org';
const emails = [...text.matchAll(emailRegex)].map(m => m[1]);
console.log(emails);

const multiline = '<tag>\nvalue\n</tag>';
console.log(/<tag>(.*?)<\/tag>/s.exec(multiline)?.[1].trim());
```

Non-greedy +? stops at the first possible match. matchAll with g flag returns all matches. The s flag makes dot match newlines.

## Key points

- Named groups: (?<name>...) added in ES2018
- Lookbehind assertions: (?<=...) and (?<!...) added in ES2018
- The d flag (hasIndices) was added in ES2022
- String.prototype.replaceAll requires the g flag or throws a TypeError

## Common follow-up questions

- What is the difference between /g flag and matchAll()?
- How do named groups improve regex maintainability?
- What does the s flag do in RegExp?

## Interview tips

- Show named groups with a real example (date parsing) - demonstrates professional regex usage
- Greedy vs non-greedy HTML parsing example is a classic interview question
- Know the 6 main flags: g, i, m, s, u, d - and what each does

## Concepts covered

Named Groups, Lookahead, Lookbehind, Flags, Non-greedy

Previous: [WebSockets and Real-time Communication](https://www.internhack.in/learn/interview/javascript-interview/js-websockets) · Next: [JavaScript Performance Optimization](https://www.internhack.in/learn/interview/javascript-interview/js-performance-optimization) · [All of JavaScript Interview](https://www.internhack.in/learn/interview/javascript-interview)

## More JavaScript Interview questions

- [Tagged Template Literals](https://www.internhack.in/learn/interview/javascript-interview/js-tagged-templates)
- [Intersection Observer and Mutation Observer](https://www.internhack.in/learn/interview/javascript-interview/js-intersection-observer)
- [Async Iterators and for await...of](https://www.internhack.in/learn/interview/javascript-interview/js-async-iterators)
- [Advanced Object Patterns and Mixins](https://www.internhack.in/learn/interview/javascript-interview/js-object-patterns)
- [ES Modules vs CommonJS](https://www.internhack.in/learn/interview/javascript-interview/js-modules-advanced)
- [Promise Combinators](https://www.internhack.in/learn/interview/javascript-interview/js-promise-combinators)

## Related

- [Learn](https://www.internhack.in/learn)
- [Interview Prep](https://www.internhack.in/learn/interview)
- [JavaScript Interview](https://www.internhack.in/learn/interview/javascript-interview)
