【问题标题】:FInd a specific string and make it bold (use Javascript or jquery)查找特定字符串并将其设为粗体(使用 Javascript 或 jquery)
【发布时间】:2022-10-14 23:48:00
【问题描述】:

我正在尝试在页面上查找特定字符串,假设我正在尝试查找字符串“ibf”。该字符串可以在任何文本元素、标题等中,因此它没有特定的类。一个页面上也可能有多个包含该字符串的元素。 这是一些示例文本

"this is ibf text"

它可能看起来像上面这样。现在我只想将 ibf 部分加粗

"this is <b>ibf</b> text"

有人可以帮帮我吗?可以使用 Javascript 或 jQuery。

【问题讨论】:

  • 我们通常期望问题包括尝试自己解决问题。我不是为你写代码

标签: javascript jquery string bold


【解决方案1】:
"this is ibf text".replace("ibf", "<b>ibf</b>")

在一般情况下:

let toFind = "ibf"
let str = "this is ibf text"
let replaced = str.replace(new RegExp(toFind, "g"), `<b>${toFind}</b>`)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-05
    • 1970-01-01
    • 1970-01-01
    • 2013-03-11
    相关资源
    最近更新 更多