Regular Expression to MATCH ALL words in a query, in any order

前端 未结 3 1745
萌比男神i
萌比男神i 2020-12-01 13:51

I\'m trying to build a search feature for a project which narrows down items based on a user search input and if it matches the keywords listed against items. For this, I\'m

3条回答
  •  情书的邮戳
    2020-12-01 14:22

    your regex looks pretty good:

    \b(meat|pasta|dinner)\b
    

    Check that the length of matches equals the number of keywords (in this case, three):

    string.match(re).length === numberOfKeywords
    

    where re is the regex with a g flag, string is the data and numberOfKeywords is the number of keywords

    This assumes that there are no repeated keywords.

提交回复
热议问题