2

I am willing to do the following:

I have :

    var distance1 = "5.5 Km";
    var distance2 = "5,5 Km";
    //The below works as expected and returns 5.5
    var finalDistance = distance1.replace( /[^\d\.]*/g, '');

    //However the below doesn't and print 55 instead
    distance2.replace( /[^\d\.]*/g, '');

    //I've tried the below too and it throws 5,5. But I want 5.5
    distance2.replace( /[^\d\.\,]*/g, '');
2
  • What is the result you are expecting and what do you mean by throws and prints? Do you mean returns? Commented Jun 27, 2017 at 0:01
  • @AliSomay in distance2 I want to see 5.5 Commented Jun 27, 2017 at 0:03

3 Answers 3

3

First, replace all occurences of , with ., then replace non-digit characters (except .) with '':

distance2 = distance2.replace( /,/g, '.').replace(/[^\d\.]+/g, '');

where:

/,/g : matches all commas ',' that will be replaced by '.'
/[^\d\.]+ : matches any sequence of non-digit and non-dot ('.') characters that will be removed (replaced by the empty string '').

The first replace transform "5,55 KM" to "5.55 KM" then the second transform the latter to "5.55".

Note: if you only have one comma, or only interested in the first encountered one, then you could use: replace(',', '.') instead of replace(/,/g, '.').

If you are using only the float representation, you could use parseFloat instead of the second replace:

var number = parseFloat(distance2.replace(/,/g, '.'));
Sign up to request clarification or add additional context in comments.

3 Comments

It works, but I would really appreciate if you can explain more what's inside the parenthesis, i.e replace( /,/g, '.') and replace( /[^\d\.]+/g, '')
@Folky.H You should do some regex tutorials if you want to understand what's going on there. It's a bit complicated if you don't know regex. Alternatively you could just use strings for the first parameter as described in my answer :P
@ibrahimmahrir Thanks a lot !
0

replace works by saying "find this string and replace with this string". The first parameter is what you want to find, the second is what to replace it with. So in your code you're replacing the , with nothing:

distance2.replace( /[^\d\.]*/g, '');

It also doesn't edit the string "in-place", so you need to assign the distance2 variable to the return value. Also, for a simple job like this you don't need to use regex. You can just input a string as the first parameter and replace will find all matches for that. This is how I would do this:

distance2 = distance2.replace(',', '.');

Further reading:

https://www.w3schools.com/jsref/jsref_replace.asp https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/replace

Comments

-1

you need to reassign the replace value to the variable.

i.e.

distance2 = distance2.replace( /[^\d\.]*/g, '');

1 Comment

This doesn't address the fact that the , is being replaced with nothing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.