JS正则表达式匹配单词,包括用空格括起来的单词

JS regexp to match words, include words surrounded by spaces

本文关键字:单词 空格 起来 包括用 正则表达式 JS      更新时间:2024-02-19

表达式:

/(^|[^ '/?$])'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

需要帮助来改进此regexp以使用ex#3。参见演示

尝试(^|[^'/])'b(foo)'b并将替换位置更改为$2bar