Does Javascript or jQuery have sometime like the "in" statement in Python?
"a" in "dea" -> True
Googling for the word in is hopeless :(
Does Javascript or jQuery have sometime like the "in" statement in Python?
"a" in "dea" -> True
Googling for the word in is hopeless :(
It does have an in
operator but is restricted to object keys only:
var object = {
a: "foo",
b: "bar"
};
// print ab
for (var key in object) {
print(key);
}
And you may also use it for checks like this one:
if ("a" in object) {
print("Object has a property named a");
}
For string checking though you need to use the indexOf() method:
if ("abc".indexOf("a") > -1) {
print("Exists");
}
Sounds like you need regular expressions!
if ("dea".match(/a/))
{
return true;
}
you would need to use indexOf
e.g
"dea".indexOf("a");
will return 2
If its not in the item then it will return -1
I think thats what you are after.
With the indexOf
function, you can extend the String like such:
String.prototype.in = function (exp) {
return exp.indexOf(this) >= 0;
}
if ("ab".in("abcde")) { //true
}