Regex in JavaScript
Literal syntax, matchAll, replaceAll, and the indices flag.
- javascript regex
- matchAll
- replaceAll
- regexp
- named groups js
Regex in JavaScript
Literal syntax, matchAll, replaceAll, and the indices flag.
Commands
| Command | What it does |
|---|---|
/pattern/gi | Literal with flags |
str.match with g flag | All matches, no group detail |
str.matchAll with named groups | Iterator with groups and indices |
str.replaceAll | Literal global replace |
d flag for indices | Match start-end positions |
Example
const re = /(?<y>\d{4})-(?<m>\d{2})/gd;
for (const m of '2026-10'.matchAll(re)) console.log(m.groups);
FAQ
match vs matchAll?
match with the g flag drops groups; matchAll keeps groups and indices - use matchAll for parsing.
How do I build patterns dynamically?
Use the RegExp constructor, but always escape user text first.
Частые вопросы
match vs matchAll?
match with the g flag drops groups; matchAll keeps groups and indices - use matchAll for parsing.
How do I build patterns dynamically?
Use the RegExp constructor, but always escape user text first.