Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

5
  • 25
    this should be the canonical answer. Because we're looking for words and not substrings, regex is appropriate. I'll also add that \b matches two things that \W doesn't, which makes it great for finding words in a string: It matches beginning of string (^) and end of string ($) Commented Oct 12, 2014 at 2:09
  • 1
    this should be the correct answer.. the rest of the answers will find "are" in a string like "do you care".. As mentioned by @Dtest Commented Jun 30, 2016 at 7:17
  • @RobertSinclair Is that so bad? If you asked me if the string "do you care" contains the word "are" I would say "yes". The word "are" is clearly a substring of that string. That's a separate question from """Is "are" one of the words in the string "do you care"""". Commented Jul 5, 2016 at 19:15
  • @Paulpro Eventhough OP didn't specify the $a is a phrase, I'm pretty sure it was implied. So his question was how to detect the Word inside the Phrase. Not if a Word contains a Word inside of it, which I would assume would be irrelevant more often than not. Commented Jul 6, 2016 at 22:08
  • @Jimbo it does works, you're just missing the `\` 3v4l.org/ZRpYi Commented Jul 24, 2018 at 9:17