JQuery string contains check

2019-01-13 04:11发布

I need to check whether a string contains another string or not?

var str1 = "ABCDEFGHIJKLMNOP";
var str2 = "DEFG";

Which function do I use to find out if str1 contains str2?

9条回答
看我几分像从前
2楼-- · 2019-01-13 04:20

Check out JavaScript's indexOf function. http://www.w3schools.com/jsref/jsref_IndexOf.asp

查看更多
叼着烟拽天下
3楼-- · 2019-01-13 04:21

Below code you can check "hello" string is exit or not in given string.

if (our_string.indexOf('hello') > -1)
{
  alert("hello found inside our_string");
}

I hope this answer is useful for you.

查看更多
一夜七次
4楼-- · 2019-01-13 04:31

I use,

var text = "some/String"; text.includes("/") <-- returns bool; true if "/" exists in string, false otherwise.

查看更多
冷血范
5楼-- · 2019-01-13 04:31

This is working for me

if (~str.indexOf("Yes"))
查看更多
神经病院院长
6楼-- · 2019-01-13 04:37

If you are worrying about Case sensitive change the case and compare the string.

 if (stringvalue.toLocaleLowerCase().indexOf("mytexttocompare")!=-1)
        {

            alert("found");
        }
查看更多
smile是对你的礼貌
7楼-- · 2019-01-13 04:44

Please try:

str1.contains(str2)
查看更多
登录 后发表回答