實現(xiàn)代碼一:
給標簽內(nèi)屬性值加上雙引號
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});};完整測試代碼
<textarea id="t" style="height:200px; width:300px;"><UL class=list><LI class=feed_item><SPAN class=note>01/12/2008</SPAN><P><A 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>實現(xiàn)代碼二:
給標簽內(nèi)屬性值加上雙引號標記變小寫
核心代碼
var partialXHTML = function (value) { return value.replace(/<(//?[a-zA-Z]+/s*)((?:"[^"]*"|'[^']*'|(?!'|"|//?>).)*)(//?>)/g, function (l, $1, $2, $3) { return '<' + $1.toLowerCase() + $2.replace(/([^=]+)=(?:(")[^"]*"|(')[^']*'|((?:(?!/s|'|"|//?>).)*))/g, function (l, $1, $2, $3, $4) { return ($2||$3) ? l : $1 + '="' + $4 + '"'; }) + $3; });};完整代碼
<textarea style="height:200px; width:300px;"><UL class=list><LI class=feed_item><SPAN class=note>01/12/2008</SPAN><P><A return value.replace(/<(//?[a-zA-Z]+/s*)((?:"[^"]*"|'[^']*'|(?!'|"|//?>).)*)(//?>)/g, function (l, $1, $2, $3) { return '<' + $1.toLowerCase() + $2.replace(/([^=]+)=(?:(")[^"]*"|(')[^']*'|((?:(?!/s|'|"|//?>).)*))/g, function (l, $1, $2, $3, $4) { return ($2||$3) ? l : $1 + '="' + $4 + '"'; }) + $3; });};var parse = function (t) { t.value = partialXHTML(t.value);};</script><input type="button" value="修正" onclick="parse(document.getElementsByTagName('textarea')[0])" />
新聞熱點
疑難解答