【发布时间】:2015-04-13 23:53:49
【问题描述】:
我在 JavaScript 中有一个字符串,我想在其中找到给定短语的所有匹配项,并用标签将它们包装起来。我无法在此处找到正确的正则表达式方法来替换不区分大小写的短语并将其替换为自身,并在其周围添加其他文本。例如:
输入字符串:
"I like to play with cats, as does Cathy, who is a member of ACATA, which is the American Cat And Tiger Association."
不区分大小写的短语:“cat”
输出字符串:
"I like to play with <em>cat</em>s, as does <em>Cat</em>hy, who is a member of A<em>CAT</em>A, which is the American <em>Cat</em> And Tiger Association."
所以,基本上,在任何匹配项周围注入<em></em>。我不能直接替换,因为我会丢失输入字符串中的原始大小写。
【问题讨论】:
标签: javascript regex