JavaScript

How to Extract a Number From a String in JavaScript

In the programming phase using JavaScript, there can be a situation to get rid of the contained garbage values in the bulk data. For instance, decoding the encoded data to utilize it or sorting the data to make it readable/understandable. In such situations, extracting a number from a string in JavaScript does wonders in utilizing the contained data and managing the memory effectively.

This blog will discuss the approaches to extract a number(s) from a string using JavaScript.

How to Extract a Number(s) From a String Using JavaScript?

To extract a number from a string in JavaScript, implement the following approaches combined with the “regular expression”:

Approach 1: Extract/Retrieve a Number From a String in JavaScript Using replace() Method

The “replace()” method locates a particular value in a string value and replaces it. This method can be utilized to replace all the contained non-digit characters with a null string and display the new value.

Syntax

string.replace(locate, new)

In the above syntax:

  • locate” corresponds to the value that needs to be replaced with the “new” value in the associated string.

 

Example

Let’s overview the following example:

<script type="text/javascript">
let string = "Linux123hint";
let extractNum = string.replace(/\D/g, '');
console.log("The extracted number is:", extractNum);
</script>

In the above code snippet:

  • Initialize the stated string value.
  • In the next step, apply the “replace()” method.
  • Its first parameter, “\D” and “g”, combined will do a global search for the non-digit characters and replace them with an empty string, specified as its second parameter.
  • Lastly, display the extracted digits from the specified string value.

 

Output

As seen in the above output, the included numbers in the string value are extracted and displayed.

Approach 2: Extract/Retrieve a Number From a String in JavaScript Using match() Method

The “match()” method matches a particular string based on a regular expression. This method can be implemented to match the numbers in a string and return the first full match.

Syntax

string.match(search)

In this syntax, “search” points to the value that needs to be searched for.

Example

The below given example explains the concept:

<script type="text/javascript">
let string = "Java 456 Script";
let extractNum = string.match(/\d+/);
console.log("The extracted number is:", extractNum[0])
</script>

In the above code block:

  • Likewise, initialize the stated string value having numbers.
  • After that, apply the “match()” method as the stated regular expression to search for the “digits(0-9)” in the string value.
  • Finally, display the resultant extracted numbers from the string.
  • Note that the associated “[0]” indicates that the outcome contains the first full match.

 

Output

The above output indicates that the desired requirement is achieved.

Conclusion

The “regular expression” in combination with the “replace()” method or the “match()” method can be utilized to extract a number from a string in JavaScript. The former approach replaces the contained string characters with an empty string, thereby extracting the numbers. The latter approach retrieves the first match from the string value that corresponds to the regular expression. This write-up explained the approaches to extract/pick out a number from a string using JavaScript.

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.