Jquery字符串格式替换

时间:2015-08-21 00:18:43

标签: jquery font-size

在整个网站中(不使用spans / divs / id标签),如何修改以下代码,以便编写H2O的所有实例,以便以较小的字体编写2。此代码目前修改了整个页面上数字2的所有实例 - 我只想搜索并替换整个网站中用H2O编写的2个。

$('body :not(script)').contents().filter(function() {
    return this.nodeType === 3;
}).replaceWith(function() {
    return this.nodeValue.replace(/[2]/g, '<font size="-1">2</font>');
});

1 个答案:

答案 0 :(得分:0)

尝试将.html(function(index, html){})String.prototype.replace()RegExp /H20/一起使用

$("body *:not(script)").html(function(_, html) {
  return html.replace(/H20/, "H<font size=-1>2</font>0")
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<div>H20 abc</div>
<span>def H20 h20 2</span>

相关问题