hi friends,
i would like to compare the strings irrespective of case type. so written the code like below.
var str1 =" HelloWorldlookatme";
var str2 = "world";
var compare1 = ((str1 !== undefined && str1 !== "") ? str1.toUpperCase() : "");
var compare2= ((str2 !== undefined && str2 !== "") ? str2.toUpperCase() : "");
var isContains = (compare1 .includes(compare2) && compare2.includes(compare1));
here if str1 is "HelloWorldlookatme" and str2 is "world" then its working but viceversa not working..
do we need to keep OR condtion instead of AND
if (isContains)
{alert("Exists");}
else
{ alert("NonExists");
}
followed above code.. still any other way to do..kindly let me know
thanks in advance

Tahir AnsariRank #366 · 6.8K reputationPosted Oct 16, 2023, 7:12 AM EDT
var str1 = "HelloWorldlookatme"; var str2 = "world"; // Convert both strings to uppercase for a case-insensitive comparison var compare1 = str1.toUpperCase(); var compare2 = str2.toUpperCase(); var isContains = compare1.includes(compare2); if (isContains) { alert("Exists"); } else { alert("NonExists"); }you can also use regular expression
var str1 = "HelloWorldlookatme"; var str2 = "world"; var regex = new RegExp(str2, 'i'); // 'i' flag for case-insensitive matching if (regex.test(str1)) { alert("Exists"); } else { alert("NonExists"); }Murali PRank #6,159 · 275 reputationPosted Oct 16, 2023, 7:38 AM EDT
hi friends,
i would like to compare the strings irrespective of case type. so written the code like below.
var str1 =" HelloWorldlookatme";
var str2 = "world";
var compare1 = ((str1 !== undefined && str1 !== "") ? str1.toUpperCase() : "");
var compare2= ((str2 !== undefined && str2 !== "") ? str2.toUpperCase() : "");
var isContains = (compare1 .includes(compare2) && compare2.includes(compare1));
here if str1 is "HelloWorldlookatme" and str2 is "world" then its working but viceversa not working..
do we need to keep OR condtion instead of AND
if (isContains)
{alert("Exists");}
else
{ alert("NonExists");
}
followed above code.. still any other way to do..kindly let me know
this code is not working if any of the string is empty.. kindly help me
thanks in advance
Tahir AnsariRank #366 · 6.8K reputationPosted Oct 16, 2023, 8:35 AM EDT
you're using the
includesmethod with bothcompare1andcompare2, and you're using the logical AND (&&) to check if both strings include each other. This means both conditions need to be true for "Exists" to be alerted. If you want to check if either of the strings contains the other, you should use the logical OR (||) instead of AND.var str1 = "HelloWorldlookatme"; var str2 = "world"; var compare1 = ((str1 !== undefined && str1 !== "") ? str1.toUpperCase() : ""); var compare2 = ((str2 !== undefined && str2 !== "") ? str2.toUpperCase() : ""); var isContains = (compare1.includes(compare2) || compare2.includes(compare1)); if (isContains) { alert("Exists"); } else { alert("NonExists"); }