作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一些文字:
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/
我有一些文字: I like chickeneggs but not normal eggs我需要包装 eggs在标签,如果整个单词是 chickeneggs但不是 eggs . 所以我的预期结果是:
我是一名优秀的程序员,十分优秀!