Skip to content
Advertisement

Advice on how to resolve an issue with extracting only numbers from a string using JavaScript

I’m new to stackoverflow and would appreciate some advice to help me solve this problem. I have two strings that I want to extract numbers from

String 1 = "12.3,Name1,3,4,Name2,35,Name3" returns [12.3,3,4,35]

Which is the required result.

String 2 = "12.3,22,Q" returns [12.322] Which is the incorrect result, It should be [12.3,22]

I have commented on my code with the steps I have taken to complete the task. Again many thanks for your advice and help in advance.

Here is a copy of my code:

JavaScript

Advertisement

Answer

The incorrect result is because of this line:

JavaScript

Once you do that, "12.3,22," (which was "12.3,22,Q" but had the Q removed before) becomes "12.322", so that looks like a single number.

I’d split first, then remove the segments that have non-number text in them, then convert to number:

JavaScript

Or without comments:

JavaScript

You could make it a single statement, but it becomes harder to debug:

JavaScript

I should note that the only reason to check that a segment is valid is that parseFloat will happily accept "123abc", returning 123 and ignoring the rest. You could use the unary + operator or the Number function instead, checking for the "" case (which would give you 0 if you didn’t weed it out):

JavaScript

My answer here goes through the various options for converting strings to numbers and the pitfalls of each of them.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement