Is there a function in jQuery or JavaScript that does the same as strstr() in PHP?
I have an AJAX response that should be 1,2,3,12,13,23 or 123. I want
Here is an implementation without any library/method calls:
function strstr (haystack, needle) {
var i = 0,
tempLength = 0,
temp = [];
for (;;) {
if (haystack[i] === undefined || needle == null) {
return "No match";
}
//if the char doesn't match then reset
else if (haystack[i] !== needle[tempLength]) {
temp = [];
tempLength = 0;
}
//the char matches so let's store it.
else if (haystack[i] === needle[tempLength]) {
temp[tempLength] = haystack[i];
if (needle[tempLength + 1] === undefined) {
return temp;
}
tempLength++;
}
i++;
}
};