在JavaScript中标识字符串是否包含任何非整数字符
JavaScript parseInt()的工作方式似乎与Java parseInt()不同 一个非常简单的例子是:在JavaScript中标识字符串是否包含任何非整数字符,javascript,regex,integer,nan,parseint,Javascript,Regex,Integer,Nan,Parseint,JavaScript parseInt()的工作方式似乎与Java parseInt()不同 一个非常简单的例子是: document.write(parseInt(" 60 ") + "<br>"); //returns 60 document.write(parseInt("40 years") + "<br>"); //returns 40 document.write(parseInt("He was 40") + "<br>"); //retu
document.write(parseInt(" 60 ") + "<br>"); //returns 60
document.write(parseInt("40 years") + "<br>"); //returns 40
document.write(parseInt("He was 40") + "<br>"); //returns NaN
document.write(parseInt(“60”)+“
”)//返回60
写作(40年)+“
”//返回40
写(parseInt(“他那时40岁”)+“
”)//返回楠
第一行可以。但我希望第2行会给出一个错误,因为实际上无法将“年”转换为整数。我相信JavaScript parseInt()只是检查字符串中的前几个字符是否为整数
那么,我如何检查字符串中只要有非整数,它就会返回NaN?parseInt是为在解析整数时提供一些灵活性而设计的。数字构造函数不太灵活,可以使用额外的字符,但也会解析非整数(谢谢Alex): 或者,使用正则表达式
" 60 ".match(/^[0-9 ]+$/); // [" 60 "]
" 60 or whatever".match(/^[0-9 ]+$/); // null
"1.24".match(/^[0-9 ]+$/); // null
parseInt在解析整数时具有一定的灵活性。数字构造函数不太灵活,可以使用额外的字符,但也会解析非整数(谢谢Alex): 或者,使用正则表达式
" 60 ".match(/^[0-9 ]+$/); // [" 60 "]
" 60 or whatever".match(/^[0-9 ]+$/); // null
"1.24".match(/^[0-9 ]+$/); // null
要检查字符串是否包含非整数,请使用正则表达式:
function(myString) {
if (myString.match(/^\d+$/) === null) { // null if non-digits in string
return NaN
} else {
return parseInt(myString.match(/^\d+$/))
}
}
要检查字符串是否包含非整数,请使用正则表达式:
function(myString) {
if (myString.match(/^\d+$/) === null) { // null if non-digits in string
return NaN
} else {
return parseInt(myString.match(/^\d+$/))
}
}
我会使用正则表达式,可能类似于下面的东西。
函数parseIntStrict(stringValue){
if(//^[\d\s]+$/.test(stringValue))//允许数字或空格
{
返回parseInt(stringValue);
}
其他的
{
返回NaN;
}
}
我会使用正则表达式,可能类似于以下内容。
函数parseIntStrict(stringValue){
if(//^[\d\s]+$/.test(stringValue))//允许数字或空格
{
返回parseInt(stringValue);
}
其他的
{
返回NaN;
}
}
最简单的方法可能是使用一元加号运算符:
var n = +str;
这也将解析浮点值。最简单的方法可能是使用一元加号运算符:
var n = +str;
这也将解析浮点值。下面是一个可以添加到所有字符串对象的
isInteger
函数:
// If the isInteger function isn't already defined
if (typeof String.prototype.isInteger == 'undefined') {
// Returns false if any non-numeric characters (other than leading
// or trailing whitespace, and a leading plus or minus sign) are found.
//
String.prototype.isInteger = function() {
return !(this.replace(/^\s+|\s+$/g, '').replace(/^[-+]/, '').match(/\D/ ));
}
}
'60'.isInteger() // true
'-60'.isInteger() // true (leading minus sign is okay)
'+60'.isInteger() // true (leading plus sign is okay)
' 60 '.isInteger() // true (whitespace at beginning or end is okay)
'a60'.isInteger() // false (has alphabetic characters)
'60a'.isInteger() // false (has alphabetic characters)
'6.0'.isInteger() // false (has a decimal point)
' 60 40 '.isInteger() // false (whitespace in the middle is not okay)
下面是一个可以添加到所有字符串对象的
isInteger
函数:
// If the isInteger function isn't already defined
if (typeof String.prototype.isInteger == 'undefined') {
// Returns false if any non-numeric characters (other than leading
// or trailing whitespace, and a leading plus or minus sign) are found.
//
String.prototype.isInteger = function() {
return !(this.replace(/^\s+|\s+$/g, '').replace(/^[-+]/, '').match(/\D/ ));
}
}
'60'.isInteger() // true
'-60'.isInteger() // true (leading minus sign is okay)
'+60'.isInteger() // true (leading plus sign is okay)
' 60 '.isInteger() // true (whitespace at beginning or end is okay)
'a60'.isInteger() // false (has alphabetic characters)
'60a'.isInteger() // false (has alphabetic characters)
'6.0'.isInteger() // false (has a decimal point)
' 60 40 '.isInteger() // false (whitespace in the middle is not okay)
为什么Javascript
parseInt
的行为应该与JavaparseInt
相同,仅仅因为它们的名称相同?它们是两种完全不同语言的函数@塞拉达:由于名称的相似性,期望JavaScript的行为类似于Java是可以理解的。当然,这种相似性是误导性的;JavaScript不是从Java派生的。这个名字显然是出于营销原因而选择的。解释了这一点。为什么JavascriptparseInt
的行为应该与JavaparseInt
相同,仅仅因为它们的名称相同?它们是两种完全不同语言的函数@塞拉达:由于名称的相似性,期望JavaScript的行为类似于Java是可以理解的。当然,这种相似性是误导性的;JavaScript不是从Java派生的。这个名字显然是出于营销原因而选择的。我不知道这是否重要,但仅供参考。Number(“1.23”)将得到1.23,parseInt(“1.23”)将只得到1,这可能很重要,也可能是irrelevant@Alex很好。感谢您提出它。parseInt(数字(“1.24”)应该给出所需的行为。“如果你问我的话,那就相当难看了。”亚历克斯,我考虑过了。我不记得Java的parseInt是如何工作的--parseInt(“1.24”)
在Java中返回1
?我相信Java Integer.parseInt(“1.24”)将返回1,但我没有实际检查。与正则表达式相比,我更喜欢数字方法,因为它还可以处理负数的“-”,我不知道这是否重要,但仅供参考。Number(“1.23”)将得到1.23,parseInt(“1.23”)将只得到1,这可能很重要,也可能是irrelevant@Alex很好。感谢您提出它。parseInt(数字(“1.24”)应该给出所需的行为。“如果你问我的话,那就相当难看了。”亚历克斯,我考虑过了。我不记得Java的parseInt是如何工作的--parseInt(“1.24”)
在Java中返回1
?我相信Java Integer.parseInt(“1.24”)将返回1,但我没有实际检查。与正则表达式相比,我更喜欢数字方法,因为它还可以处理负数的“-”。好的一点是,可以将它们添加到正则表达式中,但可能会忽略一些字符串。Op说,任何非整数字符都可以,但仍然有一个很好的观点。很好的观点是,可以将它们添加到正则表达式中,但是还有一些字符串可能会漏掉。Op说任何非整数字符,但还是不错的一点。