js addDqmForPP给标签内属性值加上双引号的函数

网络编程 发布日期:2024/10/2 浏览次数:1

正在浏览:js addDqmForPP给标签内属性值加上双引号的函数

实现代码一:

给标签内属性值加上双引号

var addDqmForPP = function(shtml){
  return shtml.replace(/( [^\=]*\=)(\s"\s\>]*)/ig,function(a,b,c,d,e){return (c)"<[^>]*"+c.replace(/(\^|\(|\)|\[|\]|\{|\}|\"[^>]*>","i").test(e))"'+c+'"':b+c:b});
};

完整测试代码

<textarea id="t" style="height:200px; width:300px;">
<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"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" _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)"<[^>]*"+c.replace(/(\^|\(|\)|\[|\]|\{|\}|\"[^>]*>","i").test(e))"'+c+'"':b+c:b});
};
alert(addDqmForPP(sa));
alert(addDqmForPP(sb));
</script>

实现代码二:

给标签内属性值加上双引号标记变小写

核心代码

var partialXHTML = function (value) {
  return value.replace(/<(\/"[^"]*"|'[^']*'|("|\/")[^"]*"|(')[^']*'|(("|\/"' + $4 + '"';
    }) + $3;
  });
};

完整代码

<textarea style="height:200px; width:300px;">
<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" _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" _eventID="5">GE石油天然气集团在中国创造新的快速安装记录</A></P></LI></UL>
</textarea>
<script type="text/javascript">
var partialXHTML = function (value) {
  return value.replace(/<(\/"[^"]*"|'[^']*'|("|\/")[^"]*"|(')[^']*'|(("|\/"' + $4 + '"';
    }) + $3;
  });
};
var parse = function (t) {
  t.value = partialXHTML(t.value);
};
</script>
<input type="button" value="修正" onclick="parse(document.getElementsByTagName('textarea')[0])" />