hash = window.location.hash.substr(1);
var reg = new RegExp('^[0-9]$');
console.log(reg.test(hash));
“123”和“123f”都是假的。我想检查哈希是否只包含数字。我错过什么了吗?
hash = window.location.hash.substr(1);
var reg = new RegExp('^[0-9]$');
console.log(reg.test(hash));
“123”和“123f”都是假的。我想检查哈希是否只包含数字。我错过什么了吗?
当前回答
您也可以使用以下方法,但要注意它们的内部实现和/或返回值。
1A isNaN(+'13761123123123'); // returns true
1B isNaN(+'13761123123ABC'); // returns false
2A ~~'1.23'; // returns 1
2B ~~'1.2A'; // returns 0
For 1A & 1B the string is first type coerced using the + operator before being passed to the isNaN() function. This works because a number types that include non-numeric values return NaN. There are considerations with the isNaN()'s implementation details which is documented here. One consideration is if a boolean value is passed as isNaN(+false|true) are coerced to their numeric equivalents and thus false is returned but one might expect the function to return true since the boolean value isn't numeric in the sense of what we are testing.
对于2A和2B,值得注意的是,寻找数字的补数要求给定的值在规范中可以引用的32位有符号整数的值范围内。
我个人的偏好是1A和1B,因为它们包含一元运算符,因此可读性较差,因为速度和简洁。
穿孔https://jsperf.com/numeric-string-test-regexvsisnan/1
其他回答
这对你的目的来说太过分了:
const numberReSnippet = "NaN|-?((\d*\.\d+|\d+)([Ee][+-]?\d+)?|Infinity)";
const matchOnlyNumberRe = new RegExp("^("+ numberReSnippet + ")$");
据我所知,这与Java和JavaScript提供给您的所有数字变体相匹配,包括“-Infinity”、“1e-24”和“NaN”。它还可以匹配您可能键入的数字,例如“-.5”。
正如所写的,numberReSnippet被设计为被放入其他正则表达式中,因此您可以提取(或避免)数字。尽管有括号,但它不包含捕获组。因此"matchOnlyNumberRe"只匹配数字类型的字符串,并为整个字符串设置了一个捕获组。
下面是Jasmine测试,你可以看到它能处理什么,不能处理什么:
describe("Number Regex", function() {
const re = matchOnlyNumberRe;
it("Matches Java and JavaScript numbers", function() {
expect(re.test( "1")).toBe(true);
expect(re.test( "0.2")).toBe(true);
expect(re.test( "0.4E4")).toBe(true); // Java-style
expect(re.test( "-55")).toBe(true);
expect(re.test( "-0.6")).toBe(true);
expect(re.test( "-0.77E77")).toBe(true);
expect(re.test( "88E8")).toBe(true);
expect(re.test( "NaN")).toBe(true);
expect(re.test( "Infinity")).toBe(true);
expect(re.test( "-Infinity")).toBe(true);
expect(re.test( "1e+24")).toBe(true); // JavaScript-style
});
it("Matches fractions with a leading decimal point", function() {
expect(re.test( ".3")).toBe(true);
expect(re.test( "-.3")).toBe(true);
expect(re.test( ".3e-4")).toBe(true);
});
it("Doesn't match non-numbers", function() {
expect(re.test( ".")).toBe(false);
expect(re.test( "9.")).toBe(false);
expect(re.test( "")).toBe(false);
expect(re.test( "E")).toBe(false);
expect(re.test( "e24")).toBe(false);
expect(re.test( "1e+24.5")).toBe(false);
expect(re.test("-.Infinity")).toBe(false);
expect(re.test( "8|24")).toBe(false);
});
});
\d将不匹配小数点。小数用下面的形式表示。
const re = /^\d*(\.\d+)?$/
'123'.match(re) // true
'123.3'.match(re) // true
'123!3'.match(re) // false
^[0-9]$
...是一个匹配任何单个数字的正则表达式,因此1将返回true,而123将返回false。
如果你加上量词*,
^[0-9]*$
表达式将匹配任意长度的数字字符串,123将返回true。(123f仍然返回false)。
请注意,从技术上讲,一个空字符串是一个长度为0的数字字符串,因此它将使用^[0-9]*$返回true。如果您只想接受包含1个或多个数字的字符串,请使用+而不是*
^[0-9]+$
正如其他人所指出的那样,有很多方法可以实现这一点,但我认为应该指出,原始问题中的代码只需要一个额外的字符就可以正常工作。
简单的Regex javascript
var cnpj = "12.32.432/1-22";
var rCnpj = cnpj.replace(/\D/gm,"");
console.log(cnpj);
*结果:
1232432122
只检查数字:
if(rCnpj === cnpj){
return true;
}
简单的例子
if("12.32.432/1-22".replace(/\D/gm,"").length > 0){
console.log("Ok.");
}
var reg = /^\d+$/;
应该这么做。原始值匹配任何恰好包含一个数字的值。