0
 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 value like ids[1] etc.

Example: str.search(/ids[i]/i);

Thanks!

4
  • Agreed with @John's answer. What's with all the string splitting and joining?
    – Matt Ball
    May 27, 2011 at 0:20
  • Please forget the splits and joinings, Its long and hard to explain all the things there. I am using ids and pins in one array and saving them all in one cookie (as array).
    – Racooon
    May 27, 2011 at 0:34
  • games is array and contains = Array("1234_12", "3333_44", "5555_4"); ids is array and contains = Array("1234", "3333", "5555");
    – Racooon
    May 27, 2011 at 0:36
  • You really should explain: 1. What you expect and 2. What you get. Simply saying "it doesn't work" is useless if we don't know what "work" is supposed to be.
    – RobG
    May 27, 2011 at 0:38

3 Answers 3

1

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.

2
  • I am searching for double values and i think this is faster then using 2 loops (IMO) and the IDS are dynamic.
    – Racooon
    May 27, 2011 at 0:25
  • @Vural Acar - Generally, no. Your regex will likely be slower than a simple double loop iteration. Although fewer lines of code implies faster, it doesn't always hold true. However, I think I'm confused as to what you're trying to do. What's in the ids array?
    – John Green
    May 27, 2011 at 0:33
0

/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'));
3
  • just put ) at the end. I ll check it now
    – Racooon
    May 27, 2011 at 0:22
  • I'm looking at your code again and it's just not making sense. Line 12 is searching for strings that match things like 'ids[0]', 'ids[1]', 'ids[2]'... you must be trying to solve the wrong problem here. Do you mind editing your question to include the actual problem this code is attempting to solve?
    – Matt Ball
    May 27, 2011 at 0:34
  • Matt example: I am searching a name in string : var str = "my name is VuRaL"; _srch = str.search(/VuRaL/i); thats all i wanted to do.
    – Racooon
    May 27, 2011 at 0:38
0

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'));
2
  • Syntaxerror in regular expression. Can you correct it please`?
    – Racooon
    May 27, 2011 at 0:20
  • @Vural: Sorry, I believe i is supposed to be a string. Try the above code again.
    – Shaz
    May 27, 2011 at 0:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.