javascript - match string against the array of regular expressions

前端 未结 8 1522
予麋鹿
予麋鹿 2020-12-02 22:23

Is there a way in JavaScript to get Boolean value for a match of the string against the array of regular expressions?

The example would be (where the \'if\' statemen

8条回答
  •  温柔的废话
    2020-12-02 23:01

    Consider breaking this problem up into two pieces:

    1. filter out the items that match the given regular expression
    2. determine if that filtered list has 0 matches in it
    const sampleStringData = ["frog", "pig", "tiger"];
    
    const matches = sampleStringData.filter((animal) => /any.regex.here/.test(animal));
    
    if (matches.length === 0) {
      console.log("No matches");
    }
    

提交回复
热议问题