Regex in JavaScript/Replacement with named groups

Use a replacement callback to access named captures clearly.

Section: JavaScript Flags and Captures

Replacement with named groups

javascript
javascript
text.replace(/(?<h>\d{2}):(?<m>\d{2})/, (...args) => { const groups = args.at(-1); return `${groups.h}h ${groups.m}m`; })
Explanation

Named groups improve readability in larger replacements.

Learn the surrounding workflow

Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.

Related commands

Same sheet · prioritizing JavaScript Flags and Captures
Named capture group
Capture with a descriptive group name.
OpenIn sheetjavascriptsame section
Named backreference in JS
Backreference a named capture.
OpenIn sheetjavascriptsame section
Replacement with numbered groups
Use `$1`, `$2`, etc. in replacement strings.
OpenIn sheetjavascriptsame section
Common JS flags
Global, ignore-case, multiline, dotAll, Unicode, sticky, indices.
OpenIn sheetjavascriptsame section
Unicode property escapes
Match Unicode letters or scripts in JS.
OpenIn sheetjavascriptsame section
String.replace()
Replace matched text using a regex.
OpenIn sheetjavascript2 tag match