How To Extract A Number From A String In JavaScript

Extract A Number From A String In JavaScript

In JavaScript, there are many built-in methods to interact with strings, and to extract a number from a string in JavaScript. In this article, I would like to introduce you two methods: replace() and match(). Let’s go into detail now.

Extract a Number from a String in JavaScript?

Use string.replace() method 

String.replace() is used to replace a string with another string.

Syntax:

string.replace(oldString, newString)

Parameters:

  • oldString: Indispensable. Is the old string you want to replace
  • newString: Indispensable. Is the string you want to change to

Example:

let myString1 = 'b13d3f5';
let myString2 = 'bdsdfkjhdf';

let replaced1 = myString1.replace(/\D/g, '');
let replaced2 = myString2.replace(/\D/g, '');

console.log(replaced1);
console.log(replaced2);

Output:

"1335"
"0"

In this example, the string myString1 and myString2, after being processed through the replace() method, has lost its non-numeric character. Because of using a regular expression (/D/g) in there, it searches the entire string and changes them to space characters so that the string is left as a string of numbers. Now we just need to convert that string to a number, and it’s done.

In case the string has no number, if we do not process it, the return result will be "". I would handle the following:

let myString = 'bdsdfkjhdf';
let replaced = myString.replace(/\D/g, '');

console.log(replaced);

Output: 

""

Use string.match() method

This method helps us to search for a string in a given string and returns an array of found string values. 

Syntax:

string.match(searchStr)

Parameters:

  • searchStr: Indispensable. Is the string we want to search

Example:

let myString = 'b13d3f5';
let replaced = myString.match(/\d+/g, '');
let num = "";

for (let x of replaced) {
    num = num + x;
}

console.log(num);

Output:

"1335"

In the example, we passed a regular expression (/d+/g). This expression means storing all numeric characters in the replace array, then merging the whole string.

I will try when the program handles and doesn’t handle the if condition to see if there’s any difference.

Example 1:

let myString = 'skjdfas';
let replaced = myString.match(/\d+/g, '');
let num = "";

for (let x of replaced) {
    num = num + x;
}

console.log(num);

Output:

TypeError: replaced is not iterable

Example 2:

let myString = 'skjdfas';
let replaced = myString.match(/\d+/g, '');
let num = "";

if (replaced !== null) {
    for (let x of replaced) {
        num = num + x;
    }
  
    console.log(num);
} else {
    console.log("There are no numeric characters in the string");
}

Output:

There are no numeric characters in the string

Like replace(), if we don’t handle the conditions, our program can get an error or output incorrect results, so it’s best to handle the conditions for them.

Summary

In this article, I have introduced two methods to extract a number from a string in JavaScript. I hope this article helps to you, and your program. Wishes you success.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *