Sometimes, we want to replace a regex capture group with uppercase in JavaScript.
In this article, we’ll look at how to replace a regex capture group with uppercase in JavaScript.
How to replace a regex capture group with uppercase in JavaScript?
To replace a regex capture group with uppercase in JavaScript, we call string replace
with a callback.
For instance, we write
const r = a.replace(/(f)/, (v) => {
return v.toUpperCase();
});
to call a
string’s replace
to find the capturing group with f
inside.
And we replace the matches values with the value converted to upper case with toUpperCase
.
Conclusion
To replace a regex capture group with uppercase in JavaScript, we call string replace
with a callback.