0

I have a string build up like varstring1_varstring2_id1_id2 eg: move_user_12_2 .

I want to extract id1 and id2 out of the string. Since I'm a complete prototype beginner I'm having some troubles solving this.

Thanks Stijn

IonuČ› G. Stan
  • 176,118
  • 18
  • 189
  • 202
Tarscher
  • 1,923
  • 1
  • 22
  • 45

3 Answers3

3

If by prototype you mean the prototype javascript framework, then what you need is the string.split method. So, in your case, the code would be something like

var myString = 'move_user_12_2';
var stringParts = myString.split('_');
var id1 = stringParts[2];
var id2 = stringParts[3];
Dan F
  • 11,958
  • 3
  • 48
  • 72
1

You could probably just use the built-in string.split() operator

var s = "move_user_12_2".split('_');
var id1 = s[2], id2 = s[3];
AgileJon
  • 53,070
  • 5
  • 41
  • 38
0

A regular expression can also be handy

var str = "move_user_12_2";
var ids = str.match(/(\d+)/g);
alert(ids[0] + "\n" + ids[1]);
epascarello
  • 204,599
  • 20
  • 195
  • 236