Reputation: 495
I have an Id of '2015-11-30_1112_3'. How do I get the values between the two underscores(_) so I am left with '1112'.
Please note that the length of the string varies.
Upvotes: 3
Views: 2485
Reputation: 68393
simplest solution would be
var value = '2015-11-30_1112_3';
alert( value.split( "_" )[ 1 ] );
just split the variable, which should give you an array of 3 items. Second item is what you are looking for
Upvotes: 8
Reputation: 1750
Write like this..
var value = "2015-11-30_1112_3";
var value1 = value.match(/_(.+)_/g);
Demo : Click here
Upvotes: 0
Reputation: 2017
Please try below solution with the help of this solution you can find value between two different symbols as well.
var str = "2015-11-30_1112_3";
var newStr = str.split('_')[1].split('_')[0];
alert(newStr);
Upvotes: -1
Reputation: 48
You can do this if you are sure that all ids have the same format:
var str= '2015-11-30_1112_3';
var array=str.split("_");
alert(array[1]);
Upvotes: 0
Reputation: 11297
You can of course use a regular expression:
s.match(/_(.*)_/)[1]
Explanation: I assume s
is your string. The expression matches everything, i.e. (.*), between two underscores. You have to select index 1 of the result because index 0 will give you the complete match including the underscores. Subsequent elements contain the bracketed groups.
Upvotes: 4