Regex match entire words only

后端 未结 7 1326
挽巷
挽巷 2020-11-21 05:35

I have a regex expression that I\'m using to find all the words in a given block of content, case insensitive, that are contained in a glossary stored in a database. Here\'s

7条回答
  •  天涯浪人
    2020-11-21 06:07

    Use word boundaries:

    /\b($word)\b/i
    

    Or if you're searching for "S.P.E.C.T.R.E." like in Sinan Ünür's example:

    /(?:\W|^)(\Q$word\E)(?:\W|$)/i
    

提交回复
热议问题