查找单词的一部分,但替换整个单词

find part of the word but replace whole word

本文关键字:单词 替换 一部分 查找      更新时间:2023-09-26
str='world is the big sorrow'
f='or'

例如,我需要用 STR 中的所有单词<b>包装,其中包含f

result='<b>world</b> is the big <b>sorrow</b>'

我需要正则表达式中最快的 js 或 mysql 解决方案

好的 - 这是你的正则表达式。

'b'w*or'w*'b

它检查单词边界后跟 0 个或多个单词字符A-Za-z0-9_。然后你的标准 - or.然后,可以选择单词字符直到单词的末尾。

在这里看到它的工作。

代码感谢阿维纳什:

var str='world is the big sorrow';
var f='or';
alert(str.replace(RegExp("(''b''w*" + f + "''w*''b)", "g"), "<b>$1</b>"))

下次,请向我们展示您自己付出了一些努力;)

问候