0

I want to remove the tags and make the selected text between the tags to upper, don't know how?

 var pattern = 'We are <orgcase>liViNg</orgcase> in a <upcase>yellow submarine</upcase>.' 
 var myRegexp = /<upcase>(.*?)<\/upcase>/g;
 var match = "$1";
 var str = pattern.replace(myRegexp, match.toUpperCase());
 console.log(str);
3
  • You need an XML/HTML parser Commented Jun 30, 2016 at 10:57
  • what is your goal ? are you making some new markup language ? Commented Jun 30, 2016 at 11:01
  • Try var str = pattern.replace(myRegexp, (m, m1) => m1.toUpperCase()); See fiddle. m1 is corresponding to (.*?) first capture group. Commented Jun 30, 2016 at 11:18

1 Answer 1

2

You need to use replace with a callback so the matched values can be fed to it and handled.

 var str = 'We are <orgcase>liViNg</orgcase> in a <upcase>yellow submarine</upcase>.' 
 var str = str.replace(/<upcase>(.*?)<\/upcase>/g, function($0) { return $0.toUpperCase(); });
 alert(str);

As for then removing the tags:

str = str.replace(/<\/?[^>]+>/g, '');

Fiddle.

Sign up to request clarification or add additional context in comments.

1 Comment

Your question remains the same, and I've answered it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.