从页面中查找长度为4的文本

时间:2016-05-07 04:43:56

标签: javascript jquery html css

我想搜索我页面上可用的所有长度为4的单词或文本,并将其替换为使用正则表达式的函数返回的其他文本

$("#changeRandom").click(function() {
      $("body").children().each(function() {
        $(this).text($(this).text().replace(/([a-zA-Z]){4}/g,"hello"));
      });

});

1 个答案:

答案 0 :(得分:-2)

你不需要在这里循环或jQuery,它可以简单地用Js完成。但是,由于您的代码中对jQuery的偏好很明显,您可以通过3个简单的步骤完成这个步骤:



<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<p>Stack Overflow is a privately held website, the flagship site of the Stack Exchange Network. Stack Overflow is a privately held website, the flagship site of the Stack Exchange Network.Stack Overflow is a privately held website, the flagship site of the Stack Exchange Network.</p>

<button onclick="replaceFunction()">Replace overflow with exchange</button>

<script>
function replaceFunction() {
  
    var repWith ="exchange";
    var content = $("body").html(); 
    var result = content.replace(/overflow/gi, repWith);
    $("body").html(result);
 
}
</script>
&#13;
&#13;
&#13;