skip to Main Content

I have this regex:

"(WORD1.*WORD2.*WORD3)|(WORD1.*WORD3.*WORD2)|(WORD2.*WORD1.*WORD3)|(WORD2.*WORD3.*WORD1)|(WORD3.*WORD1.*WORD2)|(WORD3.*WORD2.*WORD1)"

It matches to these words:

WORD1WORD2WORD3
WORD1AWORD2BWORD3C
WORD3WORD1WORD2
WORD1WORD2WORD3WORD1

But not these words:

WORD1WORD1WORD2
WORD1AWORD1BWORD2C

This regex matches when it finds a string where there are the 3 words (WORD1, WORD2, WORD3) in any order.

I would like to do the same thing with more words but the problem is that the size of the regex increases exponentially with the number of words.
Is it possible to simplify the way this regex is constructed to solve this problem (that the size does not increase exponentially) ?

3

Answers


  1. You could use positive lookahead for each of the words.

    /(?=.*WORD1)(?=.*WORD2)(?=.*WORD3).*/
    
    Login or Signup to reply.
  2. Simply iterate over all strings and filter out all those which does not include all keywords:

    (a terser version can be found in the snippet below)

    function findMatch(strings, keywords) {
      const result = [];
      
      for (const string of strings) {
        if (keywords.every(keyword => string.includes(keyword))) {
          result.push(string);
        }
      }
      
      return result;
    }
    

    Try it:

    console.config({ maximize: true });
    
    function findMatch(strings, keywords) {
      return strings.filter(
        string => keywords.every(keyword => string.includes(keyword))
      );
    }
    
    const testcases = [
      'WORD1WORD2WORD3',
      'WORD1AWORD2BWORD3C',
      'WORD3WORD1WORD2',
      'WORD1WORD2WORD3WORD1',
      'WORD1WORD1WORD2',
      'WORD1AWORD1BWORD2C'
    ];
    
    const keywords = [
      'WORD1', 'WORD2', 'WORD3'
    ];
    
    console.log(findMatch(testcases, keywords));
    <script src="https://gh-canon.github.io/stack-snippet-console/console.min.js"></script>
    Login or Signup to reply.
  3. Not sure I understood, but if I did:

    /^(?:word1|word2|word3)+$/igm
    

    Regex101.com demo

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search