JavaScript · Regular expressions

replace with function

A replacement function receives the match and captures, which makes conditional rewrites easy.

Syntax
text.replace(re, (match, …groups) => value)

Example

Try it

More in this group

/pattern/ literal Capture groups Lookahead Lookbehind Named groups regexp.exec() regexp.flags regexp.test()

Learn this in the course

Reference pages are the quick answer. The course explains the why, shows the full example and ends with a challenge.

Open the course