jQuery string contiene check


Necesito comprobar si una cadena contiene otra cadena o no?

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

¿Qué función utilizo para averiguar si str1 contiene str2?

Author: Quentin, 2010-09-16

9 answers

Puede usar la función indexOf de javascript.

var str1 = "ABCDEFGHIJKLMNOP";
var str2 = "DEFG";
if(str1.indexOf(str2) != -1){
    console.log(str2 + " found");
}
 178
Author: Rocket Hazmat,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-08-17 10:19:26
var str1 = "ABCDEFGHIJKLMNOP";
var str2 = "DEFG";

sttr1.search(str2);

Devolverá la posición del partido, o -1 si no se encuentra.

 8
Author: Khaihkd,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-02-28 02:29:13

Por favor intente:

str1.contains(str2)
 4
Author: scott,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-07-02 03:46:55

Echa un vistazo a la función indexOf de JavaScript. http://www.w3schools.com/jsref/jsref_IndexOf.asp

 3
Author: Karl Johan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-09-16 15:11:58

Utilizo,

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

 3
Author: eaglei22,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-02-14 23:03:45

Si se preocupa por distinguir entre mayúsculas y minúsculas, cambie las mayúsculas y compare la cadena.

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

            alert("found");
        }
 1
Author: mzonerz,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-07-19 10:55:11

Simplemente en el bucle foreach con usted puede comparar, separados a las cadenas

var commaseparateMainstring = 'shekar,jagadeesh,pavan,sai,suneel';
    var sustring = 'shekar,gopi,raju,suneel';
    //adding comma both sides of main string
    var main = ',' + commaseparateMainstring + ',';
    //substring change as array
    var substingArray = listToArray(sustring, ',');
    $.each(substingArray, function (index, value) {
        //calling is exist method here value means each value in substring
        var isexisted = isExist(main, value)
        if (isexisted) {
            //substring item existed
        }
        else {
        }
    });




//isExist method it checks substring exist in mainstring or not


function isExist(mainString, substring) {

if (mainString.indexOf(substring) != -1) {
    return true;
}
else {
    return false;
}
}
 0
Author: Ambala Chandrashekar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-08-25 13:05:19

Esto está funcionando para mí

if (~str.indexOf("Yes"))
 0
Author: Dev-Systematix,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-11-28 11:18:54

Debajo del código puede comprobar que la cadena "hola" es exit o no en la cadena dada.

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

Espero que esta respuesta sea útil para usted.

 0
Author: Gaurang Sondagar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-11-29 16:52:50