Hey guys I'm trying to remove two characters and any white space from a string that looks like this.
嘿家伙我正试图从一个看起来像这样的字符串中删除两个字符和任何空格。
var num1 = "34 345 324.34 $"
var num2 = "$34,345,324.34"
I basically want to remove $
and ,
and white space
我基本上想要删除$和,以及空格
so far I have this
到目前为止,我有这个
num1.replace(/,|\s/g,""); //34345324.34$
num2.replace(/,|\s/g,""); //$34345324.34
How do I also delete the $
我如何删除$
Thank you.
3 个解决方案
#1
1
You seem to be dealing with currency strings. You may follow your blacklist approach and use .replace(/[,\s$]+/g,"")
to remove all occurrences of 1+ commas, whitespaces and dollar symbols. However, once you have a pound, euro, yen, etc. currency symbol, you will have to update the regex.
你似乎在处理货币字符串。您可以按照黑名单方法使用.replace(/ [,\ s $] + / g,“”)删除所有出现的1 +逗号,空格和美元符号。但是,一旦你有一个英镑,欧元,日元等货币符号,你将不得不更新正则表达式。
Using a whitelisting approach is easier, remove all chars that are not on your whitelist, digits and dots:
使用白名单方法更容易,删除白名单上没有的所有字符,数字和点:
.replace(/[^0-9.]+/g,"")
See the regex demo.
请参阅正则表达式演示。
The [^0-9.]+
matches 1 or more occurrences of any chars other than (the [^...]
is a negated character class that matches the inverse character ranges/sets) digits and a dot.
[^ 0-9。] +匹配除([^ ...]是与逆字符范围/集匹配的否定字符类)数字和点之外的任何字符的一次或多次出现。
JS demo:
var nums = ["34 345 324.34 $", "$34,345,324.34"];
var rx = /[^0-9.]+/g;
for (var s of nums) {
console.log(s, "=>", s.replace(rx, ""));
}
#2
1
For white space in the beginning of the string (or the end) use str.trim();
, it will strip any trailing whitespaces from the beginning and/or the end of the string that contains them.
对于字符串(或结尾)开头的空格,请使用str.trim();,它将从包含它们的字符串的开头和/或末尾删除任何尾随空格。
#3
0
To remove all dots, spaces and dollars, add the dollar sign (escaped) into your regex:
要删除所有点,空格和美元,请将美元符号(转义)添加到正则表达式中:
/,|\s|\$/g
or simply
/[,\s\$]/g
Demo:
var num1 = "34 345 324.34 $"
var num2 = "$34,345,324.34"
var res1 = num1.replace(/[,\s\$]/g,""); //34345324.34
var res2 = num2.replace(/[,\s\$]/g,""); //34345324.34
console.log(res1)
console.log(res2)
#1
1
You seem to be dealing with currency strings. You may follow your blacklist approach and use .replace(/[,\s$]+/g,"")
to remove all occurrences of 1+ commas, whitespaces and dollar symbols. However, once you have a pound, euro, yen, etc. currency symbol, you will have to update the regex.
你似乎在处理货币字符串。您可以按照黑名单方法使用.replace(/ [,\ s $] + / g,“”)删除所有出现的1 +逗号,空格和美元符号。但是,一旦你有一个英镑,欧元,日元等货币符号,你将不得不更新正则表达式。
Using a whitelisting approach is easier, remove all chars that are not on your whitelist, digits and dots:
使用白名单方法更容易,删除白名单上没有的所有字符,数字和点:
.replace(/[^0-9.]+/g,"")
See the regex demo.
请参阅正则表达式演示。
The [^0-9.]+
matches 1 or more occurrences of any chars other than (the [^...]
is a negated character class that matches the inverse character ranges/sets) digits and a dot.
[^ 0-9。] +匹配除([^ ...]是与逆字符范围/集匹配的否定字符类)数字和点之外的任何字符的一次或多次出现。
JS demo:
var nums = ["34 345 324.34 $", "$34,345,324.34"];
var rx = /[^0-9.]+/g;
for (var s of nums) {
console.log(s, "=>", s.replace(rx, ""));
}
#2
1
For white space in the beginning of the string (or the end) use str.trim();
, it will strip any trailing whitespaces from the beginning and/or the end of the string that contains them.
对于字符串(或结尾)开头的空格,请使用str.trim();,它将从包含它们的字符串的开头和/或末尾删除任何尾随空格。
#3
0
To remove all dots, spaces and dollars, add the dollar sign (escaped) into your regex:
要删除所有点,空格和美元,请将美元符号(转义)添加到正则表达式中:
/,|\s|\$/g
or simply
/[,\s\$]/g
Demo:
var num1 = "34 345 324.34 $"
var num2 = "$34,345,324.34"
var res1 = num1.replace(/[,\s\$]/g,""); //34345324.34
var res2 = num2.replace(/[,\s\$]/g,""); //34345324.34
console.log(res1)
console.log(res2)