Substring with reverse index

JavascriptString

Javascript Problem Overview


How can I extract "456" from "xxx_456" where xxx is of indefinite length?

Javascript Solutions


Solution 1 - Javascript

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 - Javascript

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 - Javascript

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 - Javascript

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

Solution 5 - Javascript

alert("xxxxxxxxxxx_456".substr(-3))

caveat: according to mdc, not IE compatible

Solution 6 - Javascript

Simple regex for any number of digits at the end of a string:

'xxx_456'.match(/\d+$/)[0]; //456
'xxx_4567890'.match(/\d+$/)[0]; //4567890

or use split/pop indeed:

('yyy_xxx_45678901').split(/_/).pop(); //45678901

Solution 7 - Javascript

String.prototype.reverse( ) {
     return Array.prototype.slice.call(this)
          .reverse()
          .join()
          .replace(/,/g,'')
}

using a reverse string method

var str = "xxx_456"
str = str.reverse() // 654_xxx 
str = str.substring(0,3) // 654 
str = str.reverse() //456

if your reverse method returns the string then chain the methods for a cleaner solution.

Solution 8 - Javascript

A crazy regex approach

"xxx_456".match(/...$/)[0] //456

Solution 9 - Javascript

here is my custom function

function reverse_substring(str,from,to){
  var temp="";
  var i=0;
  var pos = 0;
  var append;      
  for(i=str.length-1;i>=0;i--){
    //alert("inside loop " + str[i]);
    if(pos == from){
		 append=true;
	}
	
	if(pos == to){
		 append=false;
		 break;
	}
	if(append){
		 temp = str[i] + temp;
	}
	pos++;
  }
  alert("bottom loop " + temp);
}

var str = "bala_123";
reverse_substring(str,0,3);

This function works for reverse index.

Solution 10 - Javascript

Also you can get the result by using substring and lastIndexOf -

alert("xxx_456".substring("xxx_456".lastIndexOf("_")+1));

Solution 11 - Javascript

Although this is an old question, to support answer by user187291

In case of fixed length of desired substring I would use substr() with negative argument for its short and readable syntax

"xxx_456".substr(-3)

For now it is compatible with common browsers and not yet strictly deprecated.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRickyView Question on Stackoverflow
Solution 1 - JavascriptAndy EView Answer on Stackoverflow
Solution 2 - JavascriptPsytronicView Answer on Stackoverflow
Solution 3 - JavascriptDarin DimitrovView Answer on Stackoverflow
Solution 4 - Javascriptghostdog74View Answer on Stackoverflow
Solution 5 - Javascriptuser187291View Answer on Stackoverflow
Solution 6 - JavascriptKooiIncView Answer on Stackoverflow
Solution 7 - Javascriptnf071590View Answer on Stackoverflow
Solution 8 - JavascriptYOUView Answer on Stackoverflow
Solution 9 - JavascriptcoderView Answer on Stackoverflow
Solution 10 - JavascriptMahfuzur RahmanView Answer on Stackoverflow
Solution 11 - JavascriptAlisaDuView Answer on Stackoverflow