VIM 正则表达式替换单词

Chr*_*ian 1 vim regex

我必须在 VIM 中使用哪个正则表达式来用单词“foo”替换文本中的所有单词“to”?(当“to”出现在“together”这样的词中时,我不想替换它)。

Joh*_*n T 7

这应该有效:

:%s/\<to\>/foo/g
Run Code Online (Sandbox Code Playgroud)

使用\<\>确保“to”只有在它是一个完整的词时才会被替换。\<匹配单词的开头和\>结尾。这样它就不会把你的“tod”变成“食物”:)