• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

294
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.

almost 3 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);

almost 3 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);

almost 3 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 Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error