我有一个文本块,其中的每个单词(从下划线开始)都应替换为另一个唯一的字符串。
例如:
_word -> _a
_anotherword -> _b
_another_word -> _c
._dotwithword -> ._d
[_brword] -> [_e]
another_word -> another_word (should stay the same)
我正在使用此正则表达式来查找它们-(_ \ w +),它可以正确替换所有内容,但如果下划线位于单词的中间,则最后一个除外。有什么办法可以通过JS regex进行检查吗?
JS小提琴进行测试:http://jsfiddle.net/C93bs/3/
非常感谢!
最佳答案
(\b_\w+)
-\b
与word boundary匹配。