比如有字符串
var string="<a href=index.html>hello</a>
<input onlick=a(); typle="submit"/>
<input onlick=a(); value=ok typle="submit"/>
<span> a=8+4</span>";希望用js将上面的字符串中HTML标签内的等号后面的没有双引号的,加上双引号。
比如onlick=a(); 要变成onlick="a();"请问,这个要怎么写呢?能用一个正则表达式搞定吗?
给个结果吧,万分感谢。
var string="<a href=index.html>hello</a>
<input onlick=a(); typle="submit"/>
<input onlick=a(); value=ok typle="submit"/>
<span> a=8+4</span>";希望用js将上面的字符串中HTML标签内的等号后面的没有双引号的,加上双引号。
比如onlick=a(); 要变成onlick="a();"请问,这个要怎么写呢?能用一个正则表达式搞定吗?
给个结果吧,万分感谢。
<head>
<script type="text/javascript">
function test()
{
var str=" <a href=\"index.html\">hello </a> <input onlick=\"a();\" typle=\"submit\"/><input onlick=\"a();\" value=\"ok\" typle=\"submit\"/> <span> a=8+4 </span>";
document.getElementById("div1").innerHTML=str;
}</script></head>
<body>
<form name="myform" action="search.htm" >
<div id="div1"></div><br/>
<input type="button" value="test" onclick="test();"/>
</form>
</body>
</html>
str = "<a href=index.html>hello </a> \
<input onlick=a(); typle='submit'/> \
<input onlick=a(); value=ok typle='submit'/> \
<span> a=8+4 </span>";
function fixHTML(str){
var tag = /<\/?[a-z][a-z0-9]*[^<>]*>/ig, fixPart = /(\w+=)(?:(['"]?)([^'"\>\/\=]+)\2\s+|(['"]?)([^'"\>\/\=]+)\4)/ig;
var tags = str.match(tag), html = str.split(tag);
for (var i = 0, j = 0; i < html.length; i++) {
if (/^\s*$/.test(html[i])) html[i] = tags[j++].replace(fixPart, '$1"$3$5" ');
}
return html.join('');
}
alert(fixHTML(str));
<textarea id="t" style="height:300px; width:600px;">
<UL class=list>
<LI class=feed_item><SPAN class=note>01/12/2008</SPAN>
<P><A title=GE公司推出“汪力和李邦”网络广告 href=http://www.genewscenter.com/{$content}\Detail.asp?ReleaseID=4886&NewsAreaID=2&changeCurrentLocale=5 _eventID="4">GE公司推出“汪力和李邦”网络广告</A></P></LI>
<LI class=feed_item><SPAN class=note>26/11/2008</SPAN>
<P><A title=GE石油天然气集团在中国创造新的快速安装记录 href="http://www.genewscenter.com/content/Detail.asp?ReleaseID=4879&NewsAreaID=2&changeCurrentLocale=5" _eventID="5">GE石油天然气集团在中国创造新的快速安装记录</A></P></LI></UL>
</textarea><script type="text/javascript">
var sa = '<a href=index.html>hello </a><input onlick=a(); typle="submit"/><input onlick=a(); value=ok typle="submit"/><span>a=8+4</span>';var sb = document.getElementById('t').value;var addDqmForPP = function(shtml){
return shtml.replace(/( [^\=]*\=)(\s?[^\"\s\>]*)/ig,function(a,b,c,d,e){return (c)?(new RegExp("<[^>]*"+c.replace(/(\^|\(|\)|\[|\]|\{|\}|\?|\-|\\|\/|\||\$)/g,'\\$1')+"[^>]*>","i").test(e))?b+'"'+c+'"':b+c:b});
};alert(addDqmForPP(sa));alert(addDqmForPP(sb));
</script>
<script>
var string1='<a href=index.html>hello</a><input onlick=a(); typle="submit"/><input onlick=a(); value=ok typle="submit"/><span></span>';
function add_quote(str){
var re,ret_str;
re = /(=[\s]*[^"\s>]+)/g;
ret_str = str.replace(re,function($1){return($1.replace("=",'="')+'"')});
return(ret_str);
}
function get_html(str){
var re,ret_str;
re = /(<[^>]+>)/g;
ret_str = str.replace(re,function($1){return(add_quote($1))});
return(ret_str);
}alert(get_html(string1));
</script>