1

I have a JavaScript variable which contains something like "fld_34_46_name". I need to be able to find the location of the THIRD _. The numbers, and name are not always the same (so, the string might also look like "fld_545425_9075_different name_test").

Is this possible? How could I do it?

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
user2370460
  • 7,470
  • 10
  • 31
  • 46

1 Answers1

4

Use the indexOf method three times:

var i = s.indexOf('_');
i = s.indexOf('_', i + 1);
i = s.indexOf('_', i + 1);

Note: If the string might contain fewer than three underscores, you would need to check for -1 after each time.

NullUserException
  • 83,810
  • 28
  • 209
  • 234
Guffa
  • 687,336
  • 108
  • 737
  • 1,005