Javascript 如何在javascript中将字符串解析为int
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4194092/
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
how to parse string to int in javascript
提问by Kobi
i want int from string in javascript how i can get them from
我想从 javascript 中的字符串获取 int 我如何从中获取它们
test1 , stsfdf233, fdfk323,
测试1,stsfdf233,fdfk323,
are anyone show me the method to get the integer from this string.
有没有人告诉我从这个字符串中获取整数的方法。
it is a rule that int is always in the back of the string.
int 总是在字符串的后面是一个规则。
how i can get the int who was at last in my string
我怎样才能得到最后在我的字符串中的 int
回答by Kobi
var s = 'abc123';
var number = s.match(/\d+$/);
number = parseInt(number, 10);
The first step is a simple regular expression - \d+$
will match the digits near the end.
On the next step, we use parseInt
on the stringwe've matched before, to get a proper number.
第一步是一个简单的正则表达式 -\d+$
将匹配末尾附近的数字。
在下一步中,我们使用之前匹配过parseInt
的字符串来获得正确的数字。
回答by T.J. Crowder
You can use a regexto extract the numbers in the string via String#match
, and convert each of them to a number via parseInt
:
您可以使用正则表达式通过 提取字符串中的数字String#match
,并通过将它们中的每一个转换为数字parseInt
:
var str, matches, index, num;
str = "test123and456";
matches = str.match(/\d+/g);
for (index = 0; index < matches.length; ++index) {
num = parseInt(matches[index], 10);
display("Digit series #" + index + " converts to " + num);
}
Ifthe numbers really occur only at the ends of the strings or you just want to convert the firstset of digits you find, you can simplify a bit:
如果数字确实只出现在字符串的末尾,或者您只想转换找到的第一组数字,则可以稍微简化一下:
var str, matches, num;
str = "test123";
matches = str.match(/\d+/);
if (matches) {
num = parseInt(matches[0], 10);
display("Found match, converts to: " + num);
}
else {
display("No digits found");
}
If you want to ignoredigits that aren't at the end, add $
to the end of the regex:
如果要忽略不在末尾的数字,请添加$
到正则表达式的末尾:
matches = str.match(/\d+$/);
回答by T.J. Crowder
var str = "stsfdf233";
var num = parseInt(str.replace(/\D/g, ''), 10);
回答by T.J. Crowder
var match = "stsfdf233".match(/\d+$/);
var result = 0; // default value
if(match != null) {
result = parseInt(match[0], 10);
}
回答by Shadow Wizard is Ear For You
Yet another alternative, this time without any replace or Regular Expression, just one simple loop:
另一种选择,这次没有任何替换或正则表达式,只是一个简单的循环:
function ExtractInteger(sValue)
{
var sDigits = "";
for (var i = sValue.length - 1; i >= 0; i--)
{
var c = sValue.charAt(i);
if (c < "0" || c > "9")
break;
sDigits = c + sDigits;
}
return (sDigits.length > 0) ? parseInt(sDigits, 10) : NaN;
}
Usage example:
用法示例:
var s = "stsfdf233";
var n = ExtractInteger(s);
alert(n);
回答by Abdennour TOUMI
Use my extension to String class :
使用我对 String 类的扩展:
String.prototype.toInt=function(){
return parseInt(this.replace(/\D/g, ''),10);
}
Then :
然后 :
"ddfdsf121iu".toInt();
Will return an integer : 121
将返回一个整数: 121
回答by br4nnigan
First positive or negative number:
第一个正数或负数:
"foo-22bar11".match(/-?\d+/); // -22
回答by Chinmayee G
This might help you
这可能会帮助你
var str = 'abc123';
var number = str.match(/\d/g).join("");
回答by Free Consulting
javascript:alert('stsfdf233'.match(/\d+$/)[0])
Global.parseInt
with radix
is overkill here, regexp extracted decimal digits already and rigth trimmed string
Global.parseInt
同radix
是矫枉过正这里,正则表达式提取的十进制数字已经和分辩修剪字符串