gpt4 book ai didi

javascript - jQuery,如果整个单词是 "egg",则将 "chickeneggs"包装在标签中

转载 作者:行者123 更新时间:2023-11-30 09:33:00 27 4
gpt4 key购买 nike

我有一些文字:

I like chickeneggs but not normal eggs我需要包装 eggs<i></i>标签,如果整个单词是 chickeneggs但不是 eggs .

所以我的预期结果是:

I like chicken<i>eggs</i> but not eggs .

我考虑过使用正则表达式,但不确定实现此目的的最佳方法。

最佳答案

匹配chickeneggs,捕获chickeneggs,然后用反向引用重新格式化;这里使用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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com