我想用点转换字符串中的所有逗号,反之亦然。但是我不知道该怎么做,因为我在第一次更改后得到所有逗号或分数。
"1.000,20" should become "1,000.20"
如何做到这一点?
答案 0 :(得分:8)
尝试
"1.000,20".replace(/[\.,]/g, function (m) { return m == '.' ? ',' : '.' })
使用replace()
答案 1 :(得分:0)
试试这个:
var str = "1,000.20";
//Split into components based on a period
var components = str.split('.');
//Iterate through each component, replacing commas with periods
var length = components.length;
for (var i = 0; i < length; i++) {
components[i] = components[i].replace(",", ".");
}
//Array.join can be slow but still useful - join with commas
str = components.join(",");