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?

2
  • Is the format always the same? Would lastIndexOf('_') work? Commented Sep 16, 2013 at 15:24
  • Hi, There might be an underscore in the name part. Commented Sep 16, 2013 at 15:24

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.