
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Pick out numbers from a string in JavaScript
We are required to write a JavaScript function that takes in a string that might contain some numbers embedded inside it.
The function should extract all the numbers from the string return the new number.
Note − If the string contains no numbers, the function should return 0.
Example
Following is the code −
const str = 'sfsd8fsdf6dsfsd8sdfs28fd0'; const pickNumbers = (str = '') => { let res = 0; for(let i = 0; i < str.length; i++){ const el = str[i]; if(+el){ res = (res * 10) + +el; }; }; return res; }; console.log(pickNumbers(str)); console.log(pickNumbers('this string contains no numbers'));
Output
Following is the output on console −
86828 0
- Related Articles
- Summing numbers from a string - JavaScript
- Filtering out numerals from string in JavaScript
- Program to pick out duplicate only once - JavaScript
- Generate n random numbers between a range and pick the greatest in JavaScript
- How to strip out HTML tags from a string using JavaScript?
- JavaScript - How to pick random elements from an array?
- Pick the synthetic fibre out of the following.CottonNylonJuteWool
- Picking all the numbers present in a string in JavaScript
- Validating a string with numbers present in it in JavaScript
- How to extract numbers from a string in Python?
- How to remove “,” from a string in JavaScript
- Building an array from a string in JavaScript
- Reversing consonants only from a string in JavaScript
- Removing adjacent duplicates from a string in JavaScript
- Remove characters from a string contained in another string with JavaScript?

Advertisements