akano1
akano1

Reputation: 41614

How to check if a text variable is contained within another variable

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

Upvotes: 1

Views: 182

Answers (3)

Amber
Amber

Reputation: 526543

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);

Upvotes: 2

Ikke
Ikke

Reputation: 101221

Javascript itself has a function for this: indexOf.

alert("blaat".indexOf('a') != -1);

Upvotes: 2

Elzo Valugi
Elzo Valugi

Reputation: 27856

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.

Upvotes: 0

Related Questions