我有一个文本块,其中的每个单词,从下划线开始,应该用另一个唯一的字符串替换。
例如:
_word -> _a
_anotherword -> _b
_another_word -> _c
._dotwithword -> ._d
[_brword] -> [_e]
another_word -> another_word (should stay the same)
我正在使用这个正则表达式找到它们 - (_ \ w +),并且它正确地替换了所有内容,除了最后一个,如果下划线位于单词的中间。有没有办法通过JS正则表达式检查这个?
JS小提琴测试:http://jsfiddle.net/C93bs/3/
非常感谢!
答案 0 :(得分:6)
(\b_\w+)
- \b
匹配word boundary。
答案 1 :(得分:3)
完整的正则表达式(适用于你的小提琴):
/\b(_\w+)\b/g
答案 2 :(得分:0)