I am trying to get the string between the first and last _ in a given string but is not working for me. Take a look to the following table with examples of input => output:
gbox_asset_locations_list => asset_locations
gbox_company_list => company
gbox_country_states_cities_list => country_states_cities
string_company_1_string => company_1
I have tried the following:
$(function() {
var str = 'gbox_asset_locations_list';
var result = str.substring(str.lastIndexOf('_') + 1, str.lastIndexOf('_'));
$('body').append(str + ' => ' + result);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
But is not working since I didn't get the proper output, can any help me to get this working? What I am doing wrong?
lastIndexOf()to locate the first underscore?=>