[Solved] How to extract number from backwards in javascript in optimised way [closed]


Here’s a simple solution using substrings:

var number = "+91-84040355236545";
var lastTenDigitsNumber = number.substr(number.length - 10);
console.log(lastTenDigitsNumber);

A simpler solution is using slice:

var number = "+91-84040355236545";
console.log(number.slice(-10));

Another solution using a function and RegEX:

function extractTenDigits(number) {
  var rx = /(\d{10}$)/g;
  var arr = rx.exec(number);
  return arr[1]; 
}

var number = "+91-84040355236545";
console.log(extractTenDigits(number));

I did a simple benchmark and the best solution for small quantity of numbers is the one using slice.

If you provide more details I can provide a more tailored suggestion.

2

solved How to extract number from backwards in javascript in optimised way [closed]