Compare part of string in JavaScript

后端 未结 7 2025
感动是毒
感动是毒 2020-12-31 01:12

How do I compare a part of a string - for example if I want to compare if string A is part of string B. I would like to find out this: When string A = \"abcd\"

7条回答
  •  离开以前
    2020-12-31 01:47

    Like this:

    var str = "abcdef";
    if (str.indexOf("abcd") >= 0)
    

    Note that this is case-sensitive. If you want a case-insensitive search, you can write

    if (str.toLowerCase().indexOf("abcd") >= 0)
    

    Or,

    if (/abcd/i.test(str))
    

    And a general version for a case-insensitive search, you can set strings of any case

    if (stringA.toLowerCase().indexOf(stringB.toLowerCase()) >= 0)
    

提交回复
热议问题