简体   繁体   中英

Regex to put quotes for html attributes

I have a scenario like this

in html tags, if the attributes is not surrounded either by single or double quotes.. i want to put double quotes for that

how to write regex for that?

If you repeat this regex as many times as there might be tags in an element, that should work so long as the text is fairly normal and not containing lots of special characters that might give false positives.

"<a href=www.google.com title = link >".replace(/(<[^>]+?=)([^"'\s][^\s>]+)/g,"$1'$2'")

Regex says: open tag ( < ) followed by one or more not close tags ( [^>]+ ) ungreedily ( ? ) followed by equals ( = ) all captured as the first group ( (...) ) and followed by second group ( (...) ) capturing not single or double quote or space ( [^"'\s] ) followed by not space or close tag ( [^\s>] ) one or more times ( + ) and then replace that with first captured group ( $1 ) followed by second captured group in single quotes ( '$2' )

For example with looping:

html = "<a href=www.google.com another=something title = link >";
newhtml = null;
while(html != newhtml){
   if(newhtml)
        html = newhtml;
   var newhtml = html.replace(/(<[^>]+?=)([^"'\s][^\s>]+)/,"$1'$2'");
}
alert(html);

But this is a bad way to go about your problem. It is better to use an HTML parser to parse, then re-format the HTML as you want it. That would ensure well formatted HTML wheras regular expressions could only ensure well formatted HTML if the input is exactly as expected.

Very helpful: I made a slight change to allow it to match attributes with a single character value: /(<[^>]+?=)([^"'\s>][^\s>]*)/g (changed one or more + to zero or more * and added > to the first match in second group).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM