Shpigford
Shpigford

Reputation: 25348

Get part of string?

I have a string formatted like this:

item_questions_attributes_abc123_id

I'm specifically trying to get the abc123 bit. That string can be any alphanumeric of any case. No special characters or spaces.

I'm using jQuery, though I'm certainly fine with using straight javascript if that's the best solution.

Upvotes: 2

Views: 6450

Answers (3)

Ovais Khatri
Ovais Khatri

Reputation: 3211

use split method of javascript and then use 2nd last index you will have your required data.

Upvotes: 0

Richard Dalton
Richard Dalton

Reputation: 35793

If it's always the 4th part of the string you can use split.

var original = 'item_questions_attributes_abc123_id';
var result = original.split('_')[3];

Upvotes: 7

Geoff
Geoff

Reputation: 9580

Try this:

var myArray = myString.split("_");
alert(myArray[3]);

Upvotes: 2

Related Questions