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

后端 未结 6 965
滥情空心
滥情空心 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:32

     const isSame = function(indx, haystack, needle){
        let j = 0;
        for(let i=indx, len = indx + needle.length; i< len; i++) {
           if (haystack[i] !== needle[j++]){
              return false;      
           }
        }
        return true;
    }
    
    var strStr = function(haystack, needle) {
        if (!needle) {
            return 0;
        }
            
        for(let i=0; i

    Base conditions:- when needle is empty string, it will return 0. If there is no match found it will return -1.

提交回复
热议问题