我有一些文字:
I like chickeneggs but not normal eggs
我需要包装 eggs
在<i></i>
标签,如果整个单词是 chickeneggs
但不是 eggs
.
所以我的预期结果是:
I like chicken<i>eggs</i> but not eggs
.
我考虑过使用正则表达式,但不确定实现此目的的最佳方法。
最佳答案
匹配chickeneggs
,捕获chicken
和eggs
,然后用反向引用重新格式化;这里使用g
进行全局匹配:
global match; find all matches rather than stopping after the first match
和 i
不分大小写匹配,查看更多信息 here :
var s = "I like chickeneggs but not normal eggs, but also ChickenEggs";
console.log(
s.replace(/(chicken)(eggs)/gi, "$1<li>$2</li>")
)
关于javascript - jQuery,如果整个单词是 "egg",则将 "chickeneggs"包装在标签中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45218365/