just wondering how I can check to see if a text variable is contained within another variable. like
var A = "J";
var B = "another J";
something like :contains but for variables.
thanks
Elzo Valugi
28.1k17 gold badges97 silver badges115 bronze badges
asked Aug 7, 2009 at 8:36
akano1
41.9k20 gold badges57 silver badges69 bronze badges
3 Answers 3
Javascript itself has a function for this: indexOf.
alert("blaat".indexOf('a') != -1);
answered Aug 7, 2009 at 8:41
Ikke
102k23 gold badges101 silver badges121 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
Assuming you mean you want to find whether the contents of A are in B, just use the following:
var found = !(B.indexOf(A) == -1);
answered Aug 7, 2009 at 8:50
Amber
532k89 gold badges643 silver badges558 bronze badges
Comments
You want to check if one string belongs into another? You can use regular expresions or strpos like function
function strpos( haystack, needle, offset){
var i = (haystack+'').indexOf(needle, (offset ? offset : 0));
return i === -1 ? false : i;
}
To my knowledge jQuery doesn't have a native function for doing what you want.
answered Aug 7, 2009 at 8:39
Elzo Valugi
28.1k17 gold badges97 silver badges115 bronze badges
Comments
lang-js