strstr()
jQuery 或 JavaScript 中是否有与PHP相同的函数?
我有一个 AJAX 响应,应该是 1、2、3、12、13、23 或 123。我想检查 1 是否存在,然后如果 2 存在,那么如果 3 存在。
strstr()
jQuery 或 JavaScript 中是否有与PHP相同的函数?
我有一个 AJAX 响应,应该是 1、2、3、12、13、23 或 123。我想检查 1 是否存在,然后如果 2 存在,那么如果 3 存在。
尝试使用这个:
function strstr(haystack, needle, bool) {
// Finds first occurrence of a string within another
//
// version: 1103.1210
// discuss at: http://phpjs.org/functions/strstr // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + bugfixed by: Onno Marsman
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// * example 1: strstr(‘Kevin van Zonneveld’, ‘van’);
// * returns 1: ‘van Zonneveld’ // * example 2: strstr(‘Kevin van Zonneveld’, ‘van’, true);
// * returns 2: ‘Kevin ‘
// * example 3: strstr(‘name@example.com’, ‘@’);
// * returns 3: ‘@example.com’
// * example 4: strstr(‘name@example.com’, ‘@’, true); // * returns 4: ‘name’
var pos = 0;
haystack += "";
pos = haystack.indexOf(needle); if (pos == -1) {
return false;
} else {
if (bool) {
return haystack.substr(0, pos);
} else {
return haystack.slice(pos);
}
}
}
(来自http://phpjs.org/functions/strstr:551)
总体而言,phpjs 非常出色。
好的,我刚刚发现了一些有用的东西!
http://my-sliit.blogspot.com/2008/06/search-string-javascript-like-strstr-in.html
感谢您的贡献:)
阅读这些 javascript 函数 - indexOF() 和 lastIndexOf()。
好吧,不是内置的。String.indexOf( String str )
返回子字符串的整数索引,但是,您可以轻松地构建一个:http ://aimtb.wordpress.com/2011/03/16/strstr-in-javascript/
function strstr(haystack, needle, bool) {
// Finds first occurrence of a string within another
//
// version: 1103.1210
// discuss at: http://phpjs.org/functions/strstr // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + bugfixed by: Onno Marsman
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// * example 1: strstr(‘Kevin van Zonneveld’, ‘van’);
// * returns 1: ‘van Zonneveld’ // * example 2: strstr(‘Kevin van Zonneveld’, ‘van’, true);
// * returns 2: ‘Kevin ‘
// * example 3: strstr(‘name@example.com’, ‘@’);
// * returns 3: ‘@example.com’
// * example 4: strstr(‘name@example.com’, ‘@’, true); // * returns 4: ‘name’
var pos = 0;
haystack += "";
pos = haystack.indexOf(needle); if (pos == -1) {
return false;
} else {
if (bool) {
return haystack.substr(0, pos);
} else {
return haystack.slice(pos);
}
}
}
这是一个没有任何库/方法调用的实现:
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++;
}
};
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<haystack.length; i++) {
if (haystack[i] === needle[0]){
if(isSame(i, haystack, needle)){
return i;
};
}
}
return -1;
};
console.log(strStr("hello", "ll"));
console.log(strStr("mississippi", "issip"));
基本条件:-当 needle 为空字符串时,它将返回 0。如果没有找到匹配项,它将返回 -1。