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
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.