function shieldStyle(){ this._styleStartArr=["<span","<p","<strong"]; } shieldStyle.prototype.show=function (str,text){ var newStr=str.replace(/^/g); newStr=newStr.replace(/undefined/g,""); var headStr=str.indexOf("<")==-1?"":newStr.substring(0,str.indexOf("<")); //var regular=/+this._styleArr[1]+/g; var otherStr=""; otherStr=newStr.replace(/</p>/g,""); otherStr=otherStr.replace(/<p>/g,""); otherStr=otherStr.replace(/<strong>/g,""); otherStr=otherStr.replace(/</strong>/g,""); otherStr=otherStr.replace(/<span>/g,""); otherStr=otherStr.replace(/</span>/g,""); otherStr=otherStr.replace(/ /g,""); otherStr=otherStr.replace(/ /g,""); otherStr=otherStr.replace(/undefined/g,""); otherStr=this.checkSign(otherStr,0,text); } shieldStyle.prototype.checkSign=function (str,id,text){ var index=str.indexOf(this._styleStartArr[id]); var newStr=str; if(index>-1){ var startStr=str.substring(0,index); var end; if(startStr==""){ end=str.indexOf(">") }else{ var endStr=str.substring(index,str.length); end=endStr.indexOf(">") } var style=str.substring(index,index+end+1); var newindex=str.indexOf(style) newStr=startStr+str.substring(str.indexOf(style)+style.length,str.length); } var isEnd=true; for(var i=0;i<this._styleStartArr.length;i++){ if(newStr.indexOf(this._styleStartArr[i])>-1){ isEnd=false; break; } } if(!isEnd){ this.checkSign(newStr,i,text); }else{ text.innerHTML=newStr; } }
正则表达式的语法比较烦,曾经很认真的看过一遍,过后就忘记了,总之这里的g是模式匹配符的一种。
g代表匹配所有相符的字符串,否则它只找到第一个就不找了。
i:ignorCase忽略大小写
m:mutiple允许多行匹配
g:globle进行全局匹配,指匹配到目标串的结尾gi:globle进行全局匹配并且忽略大小写
s:如果设定了此修正符,模式中的圆点元字符(.)匹配所有的字符,包括换行符。没有此设定的话,则不包括换行符。
x:如果设定了此修正符,模式中的空白字符除了被转义的或在字符类中的以外完全被忽略,在未转义的字符类之外的 # 以及下一个换行符之间的所有字符,包括两头,也都被忽略。
e:如果设定了此修正符,preg_replace() 在替换字符串中对逆向引用作正常的替换
style=“”则需要截取字符串,
用substring截取从0到< 的部分放在headStr。
再截取<以后的部分,把他们拼接起来,就清除了多余的部分。