2012-01-20 55 views

回答

8

尝试

"1.000,20".replace(/[\.,]/g, function (m) { return m == '.' ? ',' : '.' }) 

它使用的replace()

+0

更聪明的答案:)我在想一个正则表达式的解决方案,但忘记了回调功能。做得很好 –

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(",");