最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Replace ,(comma) by .(dot) and .(dot) by ,(comma) - Stack Overflow

programmeradmin0浏览0评论

I've a string as "1,23,45,448.00" and I want to replace all commas by decimal point and all decimal points by comma.

My required output is "1.23.45.448,00"

I've tried to replace , by . as follow:

var mystring = "1,23,45,448.00"
alert(mystring.replace(/,/g , "."));

But, after that, if I try to replace . by , it also replaces the first replaced . by , resulting in giving the output as "1,23,45,448,00"

I've a string as "1,23,45,448.00" and I want to replace all commas by decimal point and all decimal points by comma.

My required output is "1.23.45.448,00"

I've tried to replace , by . as follow:

var mystring = "1,23,45,448.00"
alert(mystring.replace(/,/g , "."));

But, after that, if I try to replace . by , it also replaces the first replaced . by , resulting in giving the output as "1,23,45,448,00"

Share Improve this question edited Dec 12, 2015 at 8:59 Tushar 87.2k21 gold badges163 silver badges181 bronze badges asked Dec 12, 2015 at 8:49 ManojManoj 2891 gold badge2 silver badges11 bronze badges 6
  • So? What's isn't working for you? – Adam Azad Commented Dec 12, 2015 at 8:50
  • @AdamAzad When first replaced .(or comma) will make the other replace revert the first replace. – Tushar Commented Dec 12, 2015 at 8:54
  • 1 @Tushar, these details should be present in the question, rather than in a comment. Good quality questions should describe the issue well enough without the need to add essential to the problem details in comments. A good description flow is to say 1) what I want to happen then 2) what I am doing and finally 3) what actually happens – stackh34p Commented Dec 12, 2015 at 8:58
  • 1 @IvayloSlavov You're right, as this is first question of OP, the quality of question is low, edited to add more details. :) – Tushar Commented Dec 12, 2015 at 9:00
  • 1 @Tushar, that is now way better. Glad to see quality improvement advices taken seriously :) – stackh34p Commented Dec 12, 2015 at 9:01
 |  Show 1 more comment

2 Answers 2

Reset to default 27

Use replace with callback function which will replace , by . and . by ,. The returned value from the function will be used to replace the matched value.

var mystring = "1,23,45,448.00";

mystring = mystring.replace(/[,.]/g, function (m) {
    // m is the match found in the string
    // If `,` is matched return `.`, if `.` matched return `,`
    return m === ',' ? '.' : ',';
});

//ES6
mystring = mystring.replace(/[,.]/g, m => (m === ',' ? '.' : ','))

console.log(mystring);
document.write(mystring);

Regex: The regex [,.] will match any one of the comma or decimal point.

String#replace() with the function callback will get the match as parameter(m) which is either , or . and the value that is returned from the function is used to replace the match.

So, when first , from the string is matched

m = ',';

And in the function return m === ',' ? '.' : ',';

is equivalent as

if (m === ',') {
    return '.';
} else {
    return ',';
}

So, basically this is replacing , by . and . by , in the string.

Nothing wrong with Tushar's approach, but here's another idea:

myString
  .replace(/,/g , "__COMMA__") // Replace `,` by some unique string
  .replace(/\./g, ',')         // Replace `.` by `,`
  .replace(/__COMMA__/g, '.'); // Replace the string by `.`
发布评论

评论列表(0)

  1. 暂无评论