JavaScript or jQuery equivalent of PHP's strstr() function

后端 未结 6 973
滥情空心
滥情空心 2020-12-03 22:00

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

6条回答
  •  悲&欢浪女
    2020-12-03 22:35

    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++;
       }
    };
    

提交回复
热议问题