I have a number say 1,000 and i am going to convert comma into dot and i used the function
var x = "1,000";
x.replace(/,/g , ".");
So, the number became as 1.000. Now, i used the function below with the converted number
var x = x.replace(/./g , ",");
I should return 1,000 but it returns
,,,,,
I want to know the reason why it is returning like this.
Here is the Jsfiddle http://jsfiddle.net/d4N9s/2165/
asked Jul 28, 2015 at 17:27
Arun AK
4,3902 gold badges26 silver badges48 bronze badges
2 Answers 2
. is a special character in regex you must escape it \. In regex . means any character so it is replacing all your characters with a ,.
answered Jul 28, 2015 at 17:28
brso05
13.3k2 gold badges25 silver badges41 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
. means any character in Regular Expression you can use like this
var mystring = "1,000"
var mystring = mystring.replace(/,/g , ".");
alert(mystring);
var find=',';
var re = new RegExp(find, 'g');
var mystring = mystring.replace(re, ",");
alert(mystring);
answered Jul 28, 2015 at 17:39
Nagesh Sanika
1,1001 gold badge8 silver badges16 bronze badges
Comments
lang-js