Categories
JavaScript Answers

How to replace a regex capture group with uppercase in JavaScript?

Spread the love

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.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.