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.
我有一个文本区域,例如,当我写“想要”时,我想用“两个”替换它。
如何匹配和替换javascript中的整个单词?
你可以使用边界这个词\b,所以\bwant\b。但是请记住,单词的正则表达式定义可能不适合您。Javascript 正则表达式将单词边界定义为单词字符\w和[a-zA-Z0-9_]非单词字符(除此之外的所有字符)之间的位置。
\b
\bwant\b
\w
[a-zA-Z0-9_]
\bcan\b
"can't"
'
\blove\b
"love_me"
e
_