Substring with reverse index

Solution 1:

slice works just fine in IE and other browsers, it's part of the specification and it's the most efficient method too:

alert("xxx_456".slice(-3));
//-> 456

slice Method (String) - MSDN
slice - Mozilla Developer Center

Solution 2:

var str = "xxx_456";
var str_sub = str.substr(str.lastIndexOf("_")+1);

If it is not always three digits at the end (and seperated by an underscore). If the end delimiter is not always an underscore, then you could use regex:

var pat = /([0-9]{1,})$/;
var m = str.match(pat);

Solution 3:

The substring method allows you to specify start and end index:

var str = "xxx_456";
var subStr = str.substring(str.length - 3, str.length);

Solution 4:

you can just split it up and get the last element

var string="xxx_456";
var a=string.split("_");
alert(a[1]); #or a.pop