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

标签 javascript jquery html css

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

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

});

最佳答案

这里不需要循环也不需要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>

关于javascript - 从页面中查找长度为 4 的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37084579/

相关文章:

JavaScript 使用箭头键 move 图像

javascript - asp.net 如何在点击文本框时显示一个div。使用 jquery

javascript - 从 firebase 获取数据到 node.js 文件

javascript - 将鼠标悬停在 td 上并添加一个类以跨越

javascript - 将 javascript 变量传递给 php,然后返回 html

JavaScript 动态对象作用域

javascript - Jquery 仅适用于 IE(高级 slider )

php - SQL查询匹配两个字段

html - 如何用占位符居中标签?

JQuery Scroll Top my image when onmousever