I notice you can actually get passed your named capture groups in the replace callback:
'Yoda said "fear leads to anger."'.replace(
/(?<pre>\b\w+\b)(?<betwixt> leads to )(?<post>\b\w+\b)/,
(...args) => {
let [{pre, betwixt, post}] = args.slice(-1);
let ani = {fear:'anger',anger:'hate',hate:'suffering'};
return `${ani[pre]}${betwixt}${ani[post]}`;
}
)
Results in Yoda said "anger leads to hate."
. At least in chrome.
My question is where is this defined? It definitely doesn't seem standardised.
It appears to be always the last parameter, after string
.
I'd like to know if it's an upcoming standard, or something whacky the chrome devs have temporarily that I accidentally discovered, and it'll disappear :(