Javascript 只返回字符串中的数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30607419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Return only numbers from string
提问by priyanka.sarkar
I have a value in Javascript as
我在 Javascript 中有一个值
var input = "Rs. 6,67,000"
How can I get only the numerical values ?
我怎样才能只得到数值?
Result:667000
结果:667000
Current Approach (not working)
当前方法(不起作用)
var input = "Rs. 6,67,000";
var res = str.replace("Rs. ", "").replace(",","");
alert(res);
Result: 667,000
回答by Alex Wayne
This is a great use for a regular expression.
这是正则表达式的一个很好的用途。
var str = "Rs. 6,67,000";
var res = str.replace(/\D/g, "");
alert(res); // 667000
\Dmatches a character that is nota numerical digit. So any non digit is replaced by an empty string. The result is only the digits in a string.
\D匹配一个不是数字的字符。所以任何非数字都被一个空字符串替换。结果只是字符串中的数字。
The gat the end of the regular expression literal is for "global" meaning that it replaces all matches, and not just the first.
将g在正则表达式文本的结尾是“全球性”,意味着它替换所有的比赛,而不仅仅是第一。
This approach will work for a variety of input formats, so if that "Rs."becomes something else later, this code won't break.
这种方法适用于各种输入格式,因此如果"Rs."以后变成其他格式,此代码不会中断。
回答by Wolfgang Kluge
You can use
您可以使用
str.replace('Rs. ', '').replace(/,/g, '');
or
或者
str.replace(/Rs. |,/g, '');
/,/gis a regular expression.gmeansglobal/Rs. |,/gis a single regular expression that matches every occurence ofRs.or,
/,/g是一个正则表达式。g方法global/Rs. |,/g是单个正则表达式,匹配Rs.或,
回答by chad.delorean
var input = "Rs. 6,67,000";
input = input.replace("Rs. ", "");
//loop through string and replace all commas
while (input.indexOf(",") !== -1) {
input = input.replace(",","");
}
回答by Adrian Grzywaczewski
For this task the easiest way to do it will be to us regex :)
对于此任务,最简单的方法是对我们使用正则表达式 :)
var input = "Rs. 6,67,000";
var res = input.replace(/\D/g,'');
console.log(res); // 667000
Here you can find more information about how to use regex:
在这里您可以找到有关如何使用正则表达式的更多信息:
https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions
https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions
I hope it helped :)
我希望它有帮助:)
Regards
问候
回答by codrrgurl84
Try this
尝试这个
var input = "ds. 7,765,000";
var cleantxt = input.replace(/^\D+/g, '');
var output = cleantxt.replace(/\,/g, "");
alert(output);
回答by Super Coder
回答by scrappedcola
You are really close. Change your replace to use the gflag, which will replace all.
你真的很亲近。更改您的替换以使用g标志,这将替换所有内容。
str.replace("Rs. ", "").replace(/,/g,"");
回答by Slipstream
If by chance you need the commayou can use the code bellow:
如果您碰巧需要逗号,您可以使用以下代码:
var input = "Rs. 6,67,000"
const numbers = str.match(/(\d|,)+/g).pop();
// 6,67,000
回答by Danielle Cohen
You can make a function like this
你可以做一个这样的功能
function justNumbers(string) {
var numsStr = string.replace(/[^0-9]/g, '');
return parseInt(numsStr);
}
var input = "Rs. 6,67,000";
var number = justNumbers(input);
console.log(number); // 667000

