[英]Simple Regex to make many zeros to single zero if no trailing digits
I want to remove any digits before '$'
symbol. 我想删除'$'
符号前'$'
所有数字。
I want to remove any '0' ','
digits after $
symbol. 我想删除$
符号后的所有'0' ','
数字。
But i don't want to remove '0's
if string is mentioned only like '$00' or '$0'
. 但是我不想删除'0's
如果仅像'$00' or '$0'
这样'$00' or '$0'
字符串被提及。
This is my Regex: home_value.replace(/,|(\\d*\\$0*)|(^0)*/g, '');
这是我的正则表达式: home_value.replace(/,|(\\d*\\$0*)|(^0)*/g, '');
I want to remove all 0's
and '$', ','
symbols from the below mentioned strings. 我想从下面提到的字符串中删除所有0's
和'$', ','
符号。 But lets say if user enter '$0' or '$000'
..it should be convert to '0'
但是可以说,如果用户输入'$0' or '$000'
则应将其转换为'0'
0045$002,34234 = 234234
$000300 = 300
$000387687 = 387687
$000 = 0
$00 = 0
$0 = 0
There's an easier way to do this than with one regex, since you're just after the dollar amount: 比起一个正则表达式,有一种更简便的方法,因为您只需要支付美元的金额即可:
var stripExcessNumbers = function (n) {
return parseInt(String(n).split("$")[1].replace(/[^\d]/g, ""), 10);
}
This should work so long as your numbers don't get too large. 只要您的数字不会太大,这就应该起作用。
test cases 测试用例
var stripExcessNumbers = function (n) { return parseInt(String(n).split("$")[1].replace(/[^\\d]/g, ""), 10); } var tests = "0045$002,34234 $000300 $000387687 $000 $00 $0".split(" ") , answers = [234234, 300, 387687, 0, 0, 0] for (var i = 0; i < tests.length; i++) { document.write("<p>" + stripExcessNumbers(tests[i]) + ", correct answer: " + answers[i] + "</p>"); }
You can use a regex like this: 您可以使用以下正则表达式:
^.*\$(0)0*$|.*\$0*|,
Look at the substitution section, you have this output: 查看替换部分,您将获得以下输出:
234234
300
387687
0
0
0
s = '2342$08934200'
+s.substr(s.indexOf('$')+1) // 8934200
s = '$0000'
+s.substr(s.indexOf('$')+1) // 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.