簡體   English   中英

Javascript用正則表達式替換點,但不起作用

[英]Javascript replace dots with regular expression, but not works

對不起,我在stackoverflow上找不到工作點替換。 人們要求更換

var str = '. 950.000.000, -';
str = str.replace(/\./gi, '');
alert(parseInt(str)); // yes, it works & will output correctly

但是,當我的'str'var是:Rp時,它不會起作用。 950.000, - 。 它是我所在地區的貨幣格式,我想用它做數學。 我這樣做,而不是工作:

var str = 'Rp. 950.000, -';
str = str.replace(/\./gi, '');// i dont know, but the str values now is nothing
alert(parseInt(str)); // sure, it outputs nothing

我只是想替換所有的點(所以它不會困擾數學運算,因為點是數字的小數)。

為什么不用\\D替換不是數字的所有內容?

var str = 'Rp. 950.000, -';
str = str.replace(/\D/gi, '');// i dont know, but the str values now is nothing
alert(parseInt(str, 10));

工作示例 http://jsfiddle.net/e8dMD/1/

刪除字符串'Rp. 950.000, -'中的所有點后'Rp. 950.000, -' 'Rp. 950.000, -'你將留下'Rp 950000, -' 如果您嘗試將此字符串用於parseInt() ,則會因為開頭的字母和結尾處的其他字符而失敗。 如果要從字符串中刪除所有非數字字符,可以使用以下內容:

str = str.replace(/\D/g, '');

在這個parseInt()應該工作正常,並給你950000

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM