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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *