Replace all the dots in a number

I’m trying to replace all dots found in a value entered by the user in an HTML form.
For instance I need the entry ‘8.30’ to be converted to ‘8×30’.

I have this simple code:

var value = $(this).val().trim(); // get the value from the form
value += ''; // force value to string
value.replace('.', 'x');

But it doesn’t work. Using the console.log command in Firebug, I can see that the replace command simply does not occur. ‘8.30’ remains the same.

I also tried the following regexp with no better result:

value.replace(/\./g, 'x');

What am I doing wrong here?

replace returns a string. Try:

value = value.replace('.', 'x');   //
                                   // or
value = value.replace(/\./g, 'x'); // replaces all '.'

You have three solutions:

var text= "ABC.DEF.XYZ";
response = text.replace(/\./g,'x');

var text= "ABC.DEF.XYZ";
response = text.replace(new RegExp("\\.","gm"),"x");

var text= "ABC.DEF.XYZ";
response = text.split('.').join('x');


The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .
Read More:   Localization of input type number

Similar Posts