JS regexp匹配单词,包括空格包围的单词

时间:2016-04-06 12:28:31

标签: javascript regex

表达式:

/(^|[^ \/?$])\b(foo)\b/g

测试字符串:

foo             - need this 1
<div>foo</div>  - need this 2
 foo            - need this 3

Foo             - dont need this
foobar          - dont need this
/foo/           - dont need this

换人:

$1bar

需要帮助来改进此正则表达式以使用前#3。See demo

1 个答案:

答案 0 :(得分:1)

尝试(^|[^\/])\b(foo)\b并将您的替换更改为$2bar