views:

277

answers:

4

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

+1  A: 

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.

Donut
A: 

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

Kheu
A: 

Try this:

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

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();
});
coderjoe