简体   繁体   中英

Regular expression to match words with no space

I am trying to do a preg_match to filter unwanted spam queries and I would like to match any word that is listed in the preg_match and filter it if it has no space after it.

So for example if I have the word balloon in the preg_match then I want to filter anything like "balloon1" or "balloond" or "balloonedfbdg" etc and allow anything with a space after balloon like "balloon big", "balloon small" etc.

I have a lot of queries from google that take a single word and add a whole bunch of crap to it that I want to filter out. It is only a few words but it is irritating for me enough to come here and find an answer to fix this.

I already use a preg_match for some of the spam queries using regular expressions but I do not know how to match something that is not spaced and allow something that has a space.

Any help is appreciated, Thanks.

Your Expression: /(balloon|otherwordone|othertwo)[^\\s]/i

This matches the listed words if they're not followed by a whitespace (\\s)

Edit: Using \\B (not a word boundary):

/(balloon|otherwordone|othertwo)\B/i

This prevents common sentence symbols from triggering the regex (like dot, comma).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM