实现代码一:
]*)/ig,function(a,b,c,d,e){return (c)?(new RegExp("<[^>]*"+c.replace(/(\^|\(|\)|\[|\]|\{|\}|\?|\-|\\|\/|\||\$)/g,'\\$1')+"[^>]*>","i").test(e))?b+'"'+c+'"':b+c:b});
};
完整测试代码
<script type="text/javascript">
var sa = 'hello <input onlick=a(); typle="submit"/><input onlick=a(); value=ok typle="submit"/>a=8+4';
var sb = document.getElementById('t').value;
var addDqmForPP = function(shtml){
return shtml.replace(/( [^\=]\=)(\s?[^\"\s>])/ig,"i").test(e))?b+'"'+c+'"':b+c:b});
};
alert(addDqmForPP(sa));
alert(addDqmForPP(sb));
实现代码二:
核心代码
).)*)(\/?>)/g,function (l,$1,$2,$3) {
return '<' + $1.toLowerCase() + $2.replace(/([^=]+)=(?:(")[^"]*"|(')[^']*'|((?:(?!\s|'|"|\/?>).)*))/g,$3,$4) {
return ($2||$3) ? l : $1 + '="' + $4 + '"';
}) + $3;
});
};
完整代码