If I have a string like so:
var str = 'Arthropoda_Arachnida_Zodariidae_Habronestes_hunti';
How can I get just the first part of the string before the last underscore?
In this case I want just 'Arthropoda_Arachnida_Zodariidae_Habronestes'
Slice and lastIndexOf:
str.slice(0, str.lastIndexOf('_'));
Combining substr and lastIndexOf should give you what you want.
var str = "Arthropoda_Arachnida_Zodariidae_Habronestes_hunti";
var start = str.substr(0, str.lastIndexof("_"));
// -> "Arthropoda_Arachnida_Zodariidae_Habronestes"
RegExp"Arthropoda_Arachnida_Zodariidae_Habronestes_hunti".replace(/(.*)_.*$/g, "$1")