javascript - 使用 JavaScript 仅查找和替换 HTML 中的整个单词?
问题描述
我想使用 javascript 替换网页中所有出现的特定单词。
示例句子 ="Hi Guys, I have a lot of things. The things are good. They are beautiful-things. Look at /home/things.html"
我在用
document.body.innerHTML =
document.body.innerHTML.replace(/\bthings\b/g, function (x) {
return 'OBJECTS';
});
但是这样替换了
Hi Guys, I have a lot of OBJECTS. The OBJECTS are good. They are beautiful-OBJECTS. Look at /home/OBJECTS.html
我只想替换整个单词。Not (beautiful-things), (/home/things.html) 不应该被替换。我只想更换(东西)。
解决方案
试试这个:
document.body.innerHTML =
document.body.innerHTML.replace(/\sthings\b/g,' OBJECTS');
Hi Guys, I have a lot of things. The things are good. They are beautiful-things. Look at /home/things.html
推荐阅读
- python - 如何使用 python 使我的网站无法访问
- powershell - 使用 powershell 调用表达式运行代码输出
- c# - UWP:如何以编程方式检索鼠标 X 按钮的行为
- java - 检查 lexer 和 parser java antlr4 中是否有任何错误
- ruby-on-rails - 为什么我的 Rails 纯文本电子邮件有时有效,有时无效
- android - 多个 Rxjava retryWhen 用于处理不同的错误
- google-cloud-platform - 如何使用 Terraform 公开 gcp 云功能
- python - 403 Google Calendar API 超出日历使用限制
- sql - SQL - 多个时间段的最大值
- html - 什么 CSP 更安全 - style-src 'none' 或 style-src 'unsafe-inline'