admin管理员组

文章数量:1286514

I am trying to replace all dots for comma and commas for dots and was wondering what is the best practice for doing this. If I do it sequentially, then the steps will overwrite each other.

For example: 1,234.56 (after replacing commas) --> 1.234.56 (after replacing dots) --> 1,234,56

Which is obviously not what I want.

One option I guess is splitting on the characters and joining afterwards using the opposite character. Is there an easier/better way to do this?

I am trying to replace all dots for comma and commas for dots and was wondering what is the best practice for doing this. If I do it sequentially, then the steps will overwrite each other.

For example: 1,234.56 (after replacing commas) --> 1.234.56 (after replacing dots) --> 1,234,56

Which is obviously not what I want.

One option I guess is splitting on the characters and joining afterwards using the opposite character. Is there an easier/better way to do this?

Share Improve this question asked Jul 25, 2014 at 20:17 Tim LimTim Lim 1181 silver badge5 bronze badges 3
  • Please give an example to work with. – erosman Commented Jul 25, 2014 at 20:21
  • 1 What do you mean? An example would simply be changing 1,234.56 to 1.234,56? – Tim Lim Commented Jul 25, 2014 at 20:23
  • I see... I wasn't sure about how you wanted it to end up... anyway it seems you already got the answer :) – erosman Commented Jul 26, 2014 at 4:39
Add a comment  | 

5 Answers 5

Reset to default 14

You could use a callback

"1,234.56".replace(/[.,]/g, function(x) {
    return x == ',' ? '.' : ',';
});

FIDDLE

If you're going to replace more than two characters, you could create a convenience function using a map to do the replacements

function swap(str, swaps) {
    var reg = new RegExp('['+Object.keys(swaps).join('')+']','g');
    return str.replace(reg, function(x) { return swaps[x] });
}

var map = {
    '.':',',
    ',':'.'
}

var result = swap("1,234.56", map); // 1.234,56

FIDDLE

You could do the following:

var str = '1,234.56';
var map = {',':'.','.':','};
str = str.replace(/[,.]/g, function(k) {
    return map[k];
});

Working Demo

Do it in stages using placeholder text:

var foo = '1,234.56';
foo = foo
    .replace(',', '~comma~')
    .replace('.', '~dot~')
    .replace('~comma~', '.')
    .replace('~dot~', ',')

You could use a for loop. Something like:

var txt = document.getElementById("txt");
var newStr = "";
for (var i = 0; i < txt.innerHTML.length; i++){
  var char = txt.innerHTML.charAt(i);
    if (char == "."){
      char = ",";   
    }else if (char == ","){
      char = ".";   
    }
    newStr += char;
}
txt.innerHTML = newStr;

Here's a fiddle: http://jsfiddle.net/AyLQt/1/

Have to say though, @adenoeo's answer is way more slick :D

In javascript you can use

var value = '1.000.000,55';
var splitValue = value.split('.');
for (var i = 0; i < splitValue.length; i++) {
    var valPart = splitValue[i];
    var newValPart = valPart.replace(',', '.');
    splitValue[i] = newValPart;
}
var newValue = splitValue.join(',');
console.log(newValue);

本文标签: javascriptReplacing commas with dot and dot with commasStack Overflow