var html='ddfsdfsdfdsd dfsdfsdffds<img _src="http://localhost:8490/60E86EA7-FE7B-44BF-8270-4DF2036B2118.PNG" data-latex="x=\frac {-b\pm \sqrt {{b}^{2}-4ac}} {2a}" src="http://localhost:8490/60E86EA7-FE7B-44BF-8270-4DF2036B2118.PNG" class="kfformula">dfsdfsrewewrererew ds<img _src="http://localhost:8490/60E86EA7-FE7B-44BF-8270-4DF2036B2118.PNG" data-latex="x=\frac {-b\pm \sqrt {{b}^{2}-4ac}} {2a}" src="http://localhost:8490/60E86EA7-FE7B-44BF-8270-4DF2036B2118.PNG" class="kfformula">dfsdfsdfsddfs<img _src="http://localhost:849060E86EA7-FE7B-44BF-8270-4DF2036B2118.PNG" data-latex="x=\frac {-b\pm \sqrt {{b}^{2}-4ac}} {2a}" src="http://localhost:8490/60E86EA7-FE7B-44BF-8270-4DF2036B2118.PNG" class="kfformula">ddfdsdfd<img _src="http://localhost:849060E86EA7-FE7B-44BF-8270-4DF2036B2118.PNG" data-latex="x=\frac {-b\pm \sqrt {{b}^{2}-4ac}} {2a}" src="http://localhost:8490/60E86EA7-FE7B-44BF-8270-4DF2036B2118.PNG" class="kfformula">dkjdfskjfdskl'; String.prototype.replaceAll = function(s1,s2){ return this.replace(new RegExp(s1,"gm"),s2); } var appendHtml=""; var keyword="s"; var partArray=html.split("<"); $.each(partArray,function(key,value){ //console.log("value="+value+";key="+key); var secPartArray=value.split('>'); $.each(secPartArray,function(key1,value1){ if(value1.indexOf("img")==0) { appendHtml+="<"+value1+">"; } else { appendHtml+=value1.replaceAll(keyword,"<strong>"+value1+"</strong>"); } console.log(value1); }); }); console.log(appendHtml);
此思路和方法可以用来突出显示文本中的内容,img标签包含的内容可以不被错误替换。