By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
Notification Show More
Latest News
Passing a JavaScript Value Between HTML Pages
Passing a JavaScript Value Between HTML Pages
JavaScript
Compare Objects in an Array
JavaScript Problem: Compare Objects in an Array
JavaScript
Switching Name Order Using Capturing Groups in Regular Expressions
Switching Name Order Using Capturing Groups in Regular Expressions
JavaScript
Shuffling an Array
JavaScript Problem: How to Perform Shuffling an Array
JavaScript
Create a Non-duplicated Collection
JavaScript Problem: Using Set to Create a Non-duplicated Collection
JavaScript
Aa
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > JavaScript > Extracting Numbers From a String in JavaScript
JavaScript

Extracting Numbers From a String in JavaScript

Admin
Last updated: 2023/01/18 at 5:22 AM
Admin
Share
4 Min Read
Extracting Numbers From a String

In this article, we are going to see the Extracting Numbers from a String in JavaScript. As we know, extracting a number from the given string will be easier when we apply some methods like parseInt() and parseFloat() methods, right? But it also has some limitation which we are going to see in here, and also we will see some different ways to extract numbers from a string.

Contents
Extracting Numbers From a String Using Traditional MethodsExtracting Numbers From a String: SolutionExtracting Numbers From a String: Solution (Alternatives)You may also like:

Extracting Numbers From a String Using Traditional Methods

Extracting Numbers From a String

As we can see, in the above simple example, first we have added a number as a string which can be extracted in multiple ways like using mathematical operations, or we can simply use parseInt() and parseFloat() methods. Which is actually simple and straight-forward when string is only a number.

Next, we have added a number with a string behind it, this number can also get extracted with the help of parseInt() and parseFloat() method since string is “99 rounds”. Here, number is in starting of the string, so extraction of number will be performed with these two methods.

Now we have a problematic string which is numbers within strings, here we have “the 99 rounds with 11 shots”, so extracting numbers from this string won’t work. So basically, parseInt() and parseFloat() methods are not able to extract the number from the string.

So we are going to solve this type of problem where numbers are given between strings.

Extracting Numbers From a String: Solution

Let’s have a basic solution to extract number is to use isNaN() method which returns a boolean result to check where a string is number or not. Here we have a string “the 99 rounds with 11 shots”, firstly we have split the string with a space(“ “), so that array will be created like [“the”, “99”, “rounds”, “with”, “11”, “shots”].

Now we have applied filter() method on this array, in which are just returning those strings which are numbers, to do this we have a method !isNaN() which checks where the element is number or not. After this, we have another array will be build named nums who has [“99”, “11”].

As we can see, the resultant array has numbers, but they are in string form, so we need to convert these strings into the number. For that, we have applied map() method on the array, and we applied parseInt() method to convert string to number.

var str = "the 99 rounds with 11 shots";

var arr = str.split(" ");

let nums = arr.filter((elem)=>{
    return !isNaN(elem);
})

let final_arr = nums.map((ele)=>{
    return parseInt(ele);
})

console.log(final_arr);

console.log(typeof(final_arr[0]));
Extracting Numbers From a String

Extracting Numbers From a String: Solution (Alternatives)

As we know, JavaScript is the flexible language, so that we can have multiple solutions for the one problem. So here are some alternatives to Extracting Numbers From a String:

Alternative 1:

let str = "the 99 rounds with 11 shots";

const result = str.split(' ').reduce((acc, cur) => {
      if (!isNaN(cur)) acc.push(+cur);
          return acc;
      }, []);
       
console.log(result);

Alternative 2:

let str = "the 99 rounds with 11 shots";
const result = str.match(/\d+/g).map(Number);
console.log(result)

Alternative 3:

var str = "the 99 rounds with 11 shots";
        
let final = str.split(' ').filter(function(n) {
    return !isNaN(n)
}).map(Number)
    
console.log(final);
       

You may also like:

  • How to Define a Class With Properties and Methods in JavaScript?
  • How to Implement Class Inheritance in JavaScript?
  • How to Find Duplicate Elements in a Given Array?

Related

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

TAGGED: Extracting Numbers From a String, Extracting Numbers From a String in javascript, javascript
Share this Article
Facebook Twitter Email Print
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Posted by Admin
Follow:
Rocoderes is a blog you can learn HTML, CSS, JavaScript, React Js and Python along with creative coding stuff and free source code files.
Previous Article Range and Sum Function Eloquent JavaScript Exercise -Range and Sum Function
Next Article Searching an Array For a Value In JavaScript, Searching an Array For a Value
Leave a comment Leave a comment

Leave a Reply Cancel reply

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

- Advertisement -

You Might Also Like

Passing a JavaScript Value Between HTML Pages

Passing a JavaScript Value Between HTML Pages

February 3, 2023
Compare Objects in an Array

JavaScript Problem: Compare Objects in an Array

January 30, 2023
Switching Name Order Using Capturing Groups in Regular Expressions

Switching Name Order Using Capturing Groups in Regular Expressions

January 29, 2023
Shuffling an Array

JavaScript Problem: How to Perform Shuffling an Array

January 27, 2023
rocoderesrocoderes
Follow US

Copyright © 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?