JavaScript/jQuery - How to check if a string contain specific words

后端 未结 9 1842
北荒
北荒 2020-12-03 10:14
$a = \'how are you\';
if (strpos($a,\'are\') !== false) {
    echo \'true\';
}

In PHP, we can use the code above to check if a string contain speci

相关标签:
9条回答
  • 2020-12-03 10:38

    If you are looking for exact words and don't want it to match things like "nightmare" (which is probably what you need), you can use a regex:

    /\bare\b/gi
    
    \b = word boundary
    g = global
    i = case insensitive (if needed)
    

    If you just want to find the characters "are", then use indexOf.

    If you want to match arbitrary words, you have to programatically construct a RegExp (regular expression) object itself based on the word string and use test.

    0 讨论(0)
  • 2020-12-03 10:40

    indexOf/includes should not be used for finding whole words:

    It does not know the difference between find a word or just a part of a word:

    "has a word".indexOf('wor')  // 6
    "has a word".includes('wor') // true
    

    Check if a single word (whole word) is in the string

    Find a real whole word, not just if the letters of that word are somewhere in the string.

    const wordInString = (s, word) => new RegExp('\\b' + word + '\\b', 'i').test(s);
    
    // tests
    [
      '',            // true
      ' ',           // true
      'did',         // true
      'id',          // flase
      'yo ',         // flase
      'you',         // true
      'you not'      // true
    ].forEach(q => console.log(
      wordInString('dID You, or did you NOt, gEt WHy?', q) 
    ))
    
    console.log(
      wordInString('did you, or did you not, get why?', 'you') // true
    )

    Check if all words are in the string

    var stringHasAll = (s, query) => 
      // convert the query to array of "words" & checks EVERY item is contained in the string
      query.split(' ').every(q => new RegExp('\\b' + q + '\\b', 'i').test(s)); 
    
    
    // tests
    [
      '',            // true
      ' ',           // true
      'aa',          // true
      'aa ',         // true
      ' aa',         // true
      'd b',         // false
      'aaa',         // false
      'a b',         // false
      'a a a a a ',  // false
    ].forEach(q => console.log(
      stringHasAll('aA bB cC dD', q) 
    ))

    0 讨论(0)
  • 2020-12-03 10:48

    You might wanna use include method in JS.

    var sentence = "This is my line";
    console.log(sentence.includes("my"));
    //returns true if substring is present.
    

    PS: includes is case sensitive.

    0 讨论(0)
  • 2020-12-03 10:48

    This will

    /\bword\b/.test("Thisword is not valid");
    

    return false, when this one

    /\bword\b/.test("This word is valid");
    

    will return true.

    0 讨论(0)
  • 2020-12-03 10:49

    you can use indexOf for this

    var a = 'how are you';
    if (a.indexOf('are') > -1) {
      return true;
    } else {
      return false;
    }
    

    Edit: This is an old answer that keeps getting up votes every once in a while so I thought I should clarify that in the above code, the if clause is not required at all because the expression itself is a boolean. Here is a better version of it which you should use,

    var a = 'how are you';
    return a.indexOf('are') > -1;
    

    Update in ECMAScript2016:

    var a = 'how are you';
    return a.includes('are');  //true
    
    0 讨论(0)
  • 2020-12-03 10:50

    var str1 = "STACKOVERFLOW";
    var str2 = "OVER";
    if(str1.indexOf(str2) != -1){
        console.log(str2 + " found");
    }

    0 讨论(0)
提交回复
热议问题