相当于 PHP 的 strstr() 函数的 JavaScript 或 jQuery
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7015181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaScript or jQuery equivalent of PHP's strstr() function
提问by medk
Is there a function in jQuery or JavaScript that does the same as strstr()
in PHP?
jQuery 或 JavaScript 中是否有与strstr()
PHP 中相同的函数?
I have an AJAX response that should be 1,2,3,12,13,23 or 123. I want to check if 1 exists, then if 2 exists then if 3 exists.
我有一个 AJAX 响应,应该是 1,2,3,12,13,23 或 123。我想检查 1 是否存在,然后如果 2 存在,那么如果 3 存在。
采纳答案by medk
Ok I just found something that works!
好吧,我刚刚找到了一些有用的东西!
http://my-sliit.blogspot.com/2008/06/search-string-javascript-like-strstr-in.html
http://my-sliit.blogspot.com/2008/06/search-string-javascript-like-strstr-in.html
Thanks for your contributions :)
感谢您的贡献:)
回答by Andrew Lee
Try using this:
尝试使用这个:
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(‘[email protected]', ‘@');
// * returns 3: ‘@example.com'
// * example 4: strstr(‘[email protected]', ‘@', 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);
}
}
}
(From http://phpjs.org/functions/strstr:551)
(来自http://phpjs.org/functions/strstr:551)
Overall phpjs is pretty phenomenal.
总体而言,phpjs 非常出色。
回答by Webars
Read about these javascript functions - indexOF() and lastIndexOf().
阅读这些 javascript 函数 - indexOF() 和 lastIndexOf()。
回答by Mrchief
Well, not built in. String.indexOf( String str )
returns the integer index of the substring
but then, you can easily build one: http://aimtb.wordpress.com/2011/03/16/strstr-in-javascript/
好吧,不是内置的。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(‘[email protected]', ‘@');
// * returns 3: ‘@example.com'
// * example 4: strstr(‘[email protected]', ‘@', 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);
}
}
}
回答by sinemetu1
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++;
}
};