Match any word containing a specific character string

I apologize in advance for the poor title of this post.

I am trying to match any word that contains a specific string of characters, that is, if I want to combine any words containing the string "click", then I want the following to be returned from my search:

  • click
  • expression
  • depression
  • pressure

So far I have this /press\w+/one that matches the word and any subsequent characters, but I don't know how to get the previous characters.

Many thanks

+3
source share
2 answers

Try

 /\w*press\w*/

* - " ", + - " ". "press".

.

+3

, , :

function findMatchingWords(t, s) {
    var re = new RegExp("\\w*"+s+"\\w*", "g");
    return t.match(re);
}

findMatchingWords("a pressed expression produces some depression of pressure.", "press");
// -> ['pressed','expression','depression','pressure']
+2

Source: https://habr.com/ru/post/1745102/