javascript - match string against the array of regular expressions

前端 未结 8 1534
予麋鹿
予麋鹿 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条回答
  •  萌比男神i
    2020-12-02 22:58

    You could use .test() which returns a boolean value when is find what your looking for in another string:

    var thisExpressions = [ '/something/', '/something_else/', '/and_something_else/'];
    var thisString = new RegExp('\\b' + 'else' + '\\b', 'i');
    var FoundIt = thisString.test(thisExpressions);  
    if (FoundIt) { /* DO STUFF */ }
    

提交回复
热议问题