Santhosh
Santhosh

Reputation: 20426

Regular expression to lower a case

i'm retrieving the innerHTML in javascript

let say element.innerHTML where all the tag are in Capital.

i need to lower only the tag.

using Regex or any inbuild methods..

Upvotes: 0

Views: 1318

Answers (4)

coderjoe
coderjoe

Reputation: 11167

This is only barely tested, and should probably be expanded upon.

Regular expressions can use function replacement in order to do more complicated replacements. If we utilize that functionality we can come up with something like:

var tag = '<DIV STYLE="WOA"></DIV>';
tag.replace(/<(\/?\w+)/g, function(r) {
   return r.toLowerCase();
});

Upvotes: 2

Donut
Donut

Reputation: 112815

You'll need to make use of toLowerCase(), in conjunction with Regular Expressions.

Try this (from here):

s = s.replace(/< *\/?(\w+)/g,function(w){return w.toLowerCase()});

Where s is the innerHTML of the element containing tags you want to replace with their lowercase equivalents.

Upvotes: 1

Gumbo
Gumbo

Reputation: 655239

Try this:

str = str.replace(/<\s*\/?\w+/g, function($0) { return $0.toLowerCase(); });

Upvotes: 0

Khodor
Khodor

Reputation: 1006

try this : </?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)/?>

Upvotes: 0

Related Questions