How can I correctly check if a string does NOT contain a specific word?

前端 未结 6 1184
一个人的身影
一个人的身影 2021-01-11 15:01

I am currently trying to figure out how to solve the above named problem. Specifically I want to check if the string does not contain the word \"stream\" both in ca

6条回答
  •  囚心锁ツ
    2021-01-11 15:11

    I would prefer to use javascript RegExp like this:

    function includesMatch(lookupValue, testString){
        var re = new RegExp(lookupValue, 'i'); //Here the 'i' means that we are doing a case insensitive match
        return testString.match(re) !== null
    }
    
    var lookup = "stream";
    var test1  = "do I have a StrEAm in me?";
    var test2  = "well at least I don't";
    console.log(includesMatch(lookup, test1));
    console.log(includesMatch(lookup, test2));

提交回复
热议问题