Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace all the dots in a number

Tags:

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 '8x30'.

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?

like image 509
s427 Avatar asked Oct 29 '10 08:10

s427


1 Answers

replace returns a string. Try:

value = value.replace('.', 'x');   //                                    // or value = value.replace(/\./g, 'x'); // replaces all '.' 
like image 160
Bart Kiers Avatar answered Sep 22 '22 14:09

Bart Kiers