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

Introduction

When dealing with large amounts of data, it is important to be able to extract numbers from a string in an efficient and optimized way. This is especially true when dealing with strings that are written in reverse order. In this article, we will discuss how to extract numbers from a backwards string in JavaScript in an optimized way. We will look at various methods and techniques that can be used to achieve this goal. We will also discuss the pros and cons of each approach and provide examples of code that can be used to implement the solutions.

Solution

// Using a regular expression
const extractNumber = str => str.match(/\d+/g).map(Number).reverse();

// Using a loop
const extractNumber = str => {
let result = [];
for (let i = str.length – 1; i >= 0; i–) {
if (str[i] >= ‘0’ && str[i] <= '9') { result.push(Number(str[i])); } } return result; };


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]


If you are looking for an optimized way to extract numbers from a string in JavaScript, then you have come to the right place. In this article, we will discuss how to extract numbers from a string in an optimized way using JavaScript.

The first step is to create a regular expression that will match the numbers in the string. This can be done by using the following code:

var regex = /[0-9]+/g;

This regular expression will match any sequence of one or more digits. Once the regular expression is created, we can use it to extract the numbers from the string. This can be done by using the following code:

var numbers = string.match(regex);

This code will return an array of all the numbers that were found in the string. If you want to extract the numbers in reverse order, then you can use the following code:

var numbers = string.match(regex).reverse();

This code will return an array of all the numbers that were found in the string, but in reverse order. This is an optimized way to extract numbers from a string in JavaScript.

We hope this article has helped you understand how to extract numbers from a string in an optimized way using JavaScript. If you have any questions or comments, please feel free to leave them in the comments section below.