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. any ideas? well line 12 works, but does not return the result correctly.
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); only VuRaL needs to be an array val开发者_StackOverflow中文版ue like ids[1] etc.
Example: str.search(/ids[i]/i);
Thanks!
Um. Line 8 converts your array into a string. Line 10 has you iterating over this string on a character by character basis. Are you trying to find something in the overall string? You don't need your for(){} loop if so.
/ids[i]/
is a regex literal. It's not actually inserting the value of the variable i
. Also, [
and ]
have special meaning in JavaScript regexes, so you need to escape them. Replace line 12 with this:
_srch = idsList.search(new RegExp('ids\\[' + i + '\\]', 'i'));
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'));
精彩评论