[英]JS, Searching in array doesnt work
0. <script type="text/javascript">
1. var games = new GameList("bets");
2. games = games.getGames(); //an Array, example: games = ("1313_55", "3353_65");
3.
4. var game_ids = $.map(games, function(_g) {
5. return _g.split('_')[0];
6. });
7.
8. idsList = game_ids.join(",");
9. var _srch = -1;
10. for(var i = 0, j = idsList.length; i < j && _srch == -1; i++)
11. {
12. _srch = idsList.search(/ids[i]/i);
13. }
14. </script>
line 12 doesnt work.第 12 行不起作用。 any ideas?有任何想法吗? well line 12 works, but does not return the result correctly.第 12 行很好,但没有正确返回结果。
what i am trying to do: Example:我正在尝试做的事情:示例:
I am searching a name in string:我正在搜索字符串中的名称:
var str = "my name is VuRaL";
_srch = str.search(/VuRaL/i);
//thats what i want to do. //这就是我想做的。
str.search(/VuRaL/i); str.search(/VuRaL/i); only VuRaL needs to be an array value like ids[1] etc.只有 VuRaL 需要是一个数组值,如 ids[1] 等。
Example: str.search(/ids[i]/i);示例:str.search(/ids[i]/i);
Thanks!谢谢!
Um.嗯。 Line 8 converts your array into a string.第 8 行将您的数组转换为字符串。 Line 10 has you iterating over this string on a character by character basis.第 10 行让您逐个字符地迭代此字符串。 Are you trying to find something in the overall string?你想在整个字符串中找到一些东西吗? You don't need your for(){} loop if so.如果是这样,你不需要你的 for(){} 循环。
Is this what you're going for?这就是你想要的吗?
_srch = idsList.search(new RegExp("ids[" + i + "]", 'i'));
In case you want to search for brackets themselves, you'll need to escape them like so:如果您想自己搜索括号,则需要像这样转义它们:
_srch = idsList.search(new RegExp("ids\\[" + i + "\\]", 'i'));
/ids[i]/
is a regex literal. /ids[i]/
是一个正则表达式文字。 It's not actually inserting the value of the variable i
.它实际上并没有插入变量i
的值。 Also, [
and ]
have special meaning in JavaScript regexes, so you need to escape them.此外, [
和]
在 JavaScript 正则表达式中具有特殊含义,因此您需要对其进行转义。 Replace line 12 with this:将第 12 行替换为:
_srch = idsList.search(new RegExp('ids\\[' + i + '\\]', 'i'));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.