【发布时间】:2017-10-18 17:50:09
【问题描述】:
我需要编写第二个 RegExp 以在 sentence 内找到不在标签中的变量 d。所以标签中的变量应该被跳过。
Regex '(?:^|\\b)('+d+')(?=\\b|$)' 会找到 d 变量,但我需要用 class="description" 排除 <span> 标记。
新句子被包裹在新标签中。
sentence = "This is some word. <span class='description'>word</span> in tag should be skipped"
d = 'word'
re = new RegExp('(?:^|\\b)('+d+')(?=\\b|$)', 'gi')
sentence = sentence.replace(re, "<span>$1</span>")
我想要达到的结果是:
"This is some <span>word</span>. <span class='description'>word</span> in tag should be skipped"
我正在使用coffeescript,感谢您的帮助。
【问题讨论】:
-
您的字符串文字无效。并且
RegExpt应该没有t(除非你有一个同名的函数) -
@trincot 谢谢,我是 RegExp 的新手,你能帮忙用这个例子吗?
-
请花点时间阅读this masterpiece。
-
@DineiRockenbach 有没有其他方法可以在字符串中查找单词(后跟逗号、点等(某些规则))并将其包装在新标签中?谢谢
-
@user7754069 您的字符串是 XML/HTML 摘录还是带有一些标签的随机字符串?
标签: javascript regex