比如有字符串
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();"请问,这个要怎么写呢?能用一个正则表达式搞定吗?
给个结果吧,万分感谢。
  

解决方案 »

  1.   

    " 用 \" 替代<html>
    <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>
      

  2.   


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

  3.   

    我觉得楼主的意思大概是用js修复书写不规范的网页...大概就是要自动识别htm标签...然后对标签里每个元素属性的等号后面的值判断是不是加了双引号...没有的就用js给他加上...而不是问js用什么来表示双引号...其实ff和opera浏览器都可以自动修复这些问题代码然后正常解析...也就是按照有引号来操作的(op对value后面的值可能不处理)......ie就不知道了...
      

  4.   

    参见http://www.jslab.org.cn/?tag=addDqmForPP 给标签内属性值加上双引号
    <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&amp;NewsAreaID=2&amp;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&amp;NewsAreaID=2&amp;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>
      

  5.   


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