preg_match 点也匹配非点

preg_match with dot matches non-dot too

我正在尝试将 .com(以及 word@)与 preg_match 匹配,如下所示:

if(preg_match("/(word|.com|\@)/i", $content)){
   // dance here
}

问题是它也与单词 completely 匹配。它应该只匹配 .com。我该如何解决?

. 是 "any single character" 的通配符。因为你 watch 是为了匹配文字 .,所以你必须转义它:

if(preg_match("/(word|\.com|\@)/i", $content)){
                      ^---