0

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

2 Answers 2

7

. 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
Sign up to request clarification or add additional context in comments.

Comments

0

. 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

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.