Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
在正则表达式中,[]表示“这些字符之一”,因此/[|b|]/表示|OR b。
[]
/[|b|]/
|
b
你想要/\|b\|/g。没有g,它只替换一次。
/\|b\|/g
g
您正在声明一个字符类,[|b|]它意味着匹配bor |。您需要转义管道\|b\|,因为管道在正则表达式中表示“或”。
[|b|]
\|b\|
替换为的正确正则|b|表达式<b>是
|b|
<b>
/\|b\|/
见:http: //jsfiddle.net/vfTG4/