首页 > 开发 > JS > 正文

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

2024-05-06 16:34:06
字体:
来源:转载
供稿:网友

实现代码一:

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

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>

实现代码二:

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

核心代码

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])" />

 

 

注:相关教程知识阅读请移步到JavaScript/Ajax教程频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表