Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

390
Views
Javascript replace a capture group to uppercase (not a duplicate)

I am aware of Replace a Regex capture group with uppercase in Javascript

That is for replacing the entire match and not a () match.

Here is my problem:

var text = '<span style="font-variant: small-caps" class="small-caps">Lord</span>'
text = text.replaceAll(/<span style="font-variant.*?>(.*)<\/span>/g, function (v) { return v.toUpperCase(); });
console.log(text);

This returns the entire tag as uppercase and not the actual text in (.*).

I just want to replace the span tag with just the uppercase of the innertext. There is more than one span tag as well in the actual text variable.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Capture groups are passed as additional arguments to the callback function. So use the argument with the capture.

var text = '<span style="font-variant: small-caps" class="small-caps">Lord</span>'
text = text.replaceAll(/<span style="font-variant.*?>(.*)<\/span>/g,
  (match, g1) => g1.toUpperCase());
console.log(text);

about 4 years ago · Juan Pablo Isaza Report

0

You can also use />(.*)<\/span>/g

var text =
  '<span style="font-variant: small-caps" class="small-caps">Lord</span>';

text = text.replaceAll(/>(.*)<\/span>/g, function(_, group) {
  return `>${group.toUpperCase()}</span>`;
});
console.log(text);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!