日期:2014-05-18 浏览次数:20747 次
<script> var keywords=[];//要替换的部分 var replaces=[];//替换成什么 var signs=[];//特定标记,不用管它 keywords[0]=/结婚用品/g; keywords[1]=/结婚/g; replaces[0]="<a href='http://www.sina.com.cn'>结婚用品</a>"; replaces[1]="<a href='http://www.163.com'>结婚</a>"; function convert(){ var contentValue=content.innerHTML; //设置signs,并替换为signs for(var i=0;(e=keywords[i]);i++){ signs[i]="thePartWhichWillBeReplace"+i; contentValue=contentValue.replace(keywords[i],signs[i]); } for(var i=0;(e=signs[i]);i++){ while(contentValue.indexOf(signs[i])>=0){ contentValue=contentValue.replace(signs[i],replaces[i]); } } content.innerHTML=contentValue; } </script>
------解决方案--------------------
<script type="text/javascript"> var a='aaabaa<img src="xxx.gif" alt="aaabaa">aaabaa<img src="xxx.gif" alt="aaabaa">aaabaa<Span>ssss<Span>ssss</Span>aaabaa</Span>' alert(a.replace(/(^|>)([^>]*?)aaab([^>]*?)(^|<)/gi,"$1$2mm$3$4")) </script>