
- 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
Function that parses number embedded in strings - JavaScript
Conventionally, we have functions like parseInt() and parseFloat() that takes in a string and converts the number string to Number. But these methods fail when we have numbers embedded at random index inside the string.
For example: The following would only return 454, but what we want is 4545453 −
parseInt('454ffdg54hg53')
So, we are required to write a JavaScript function that takes in such string and returns the corresponding number.
Example
Following is the code −
const numStr = '454ffdg54hg53'; const parseInteger = numStr => { let res = 0; for(let i = 0; i < numStr.length; i++){ if(!+numStr[i]){ continue; }; res = (res * 10) + (+numStr[i]); }; return res; }; console.log(parseInteger(numStr));
Output
Following is the output in the console −
4545453
- Related Articles
- Function to find out palindrome strings JavaScript
- JavaScript Number Function
- Group strings starting with similar number in JavaScript
- Function to check two strings and return common words in JavaScript
- Add number strings without using conversion library methods in JavaScript
- Counting the number of letters that occupy their positions in the alphabets for array of strings using JavaScript
- Formatted Strings Using Template Strings in JavaScript
- How to call a function that returns another function in JavaScript?
- Currified function that multiples array elements in JavaScript
- Template strings in JavaScript.
- How to match strings that aren't entirely digits in JavaScript?
- Enter a number and write a function that adds the digits together on button click in JavaScript
- Function to compute factorial of a number in JavaScript
- Number of digits that divide the complete number in JavaScript
- Nesting template strings in JavaScript

Advertisements