Boncho Belutov
Boncho Belutov

Reputation: 23

JavaScript regex

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

Upvotes: 0

Views: 59

Answers (1)

Mitya
Mitya

Reputation: 34556

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.

Upvotes: 2

Related Questions