You could use positive lookahead for each of the words.
/(?=.*WORD1)(?=.*WORD2)(?=.*WORD3).*/
The more performant version below specifies a starting anchor and only matches a single character after verifying lookaheads. This technique is intended only for matching
, as requested by the OP, and not intended for extraction
.
/^(?=.*WORD1)(?=.*WORD2)(?=.*WORD3)./
A positive lookahead acts like a gate in that it proceeds only if the specified match within its parentheses exists, but it doesn't consume or capture what it matches-- it's always zero-length. If you "look ahead" to see if each word exists preceded by anything .*
, then it doesn't matter what order those words are in. You proceed if true for each word without the match using up any content.
If you're only concerned with whether the content matches or not, then the only material difference between the two expressions is how long they take. Let's say you have only 2 of the 3 required words in your content. Unless the software that interprets the expression can recognize the futility of trying, it may look for the three words at the first position, fail, then try at the second position, fail, etc. until it reaches the last position before it gives up. By specifying ^
, it will only check at the first position, saving the time of the other unnecessary checks. Removing the *
from the end prevents some unnecessary capture when you're just looking for a true/false answer of whether all your words exist in the content.