正则表达式检查字符串是否只包含数字 [英] Regex to check whether a string contains only numbers

查看:497
本文介绍了正则表达式检查字符串是否只包含数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hash = window.location.hash.substr(1);
var reg = new RegExp('^[0-9]$');
console.log(reg.test(hash));

我在123上都是假的和123f。我想检查散列是否只包含数字。我错过了什么吗?

I get false on both "123" and "123f". I would like to check if the hash only contains numbers. Did I miss something?

推荐答案

var reg = /^\d+$/;

应该这样做。原文匹配任何由一位数组成的内容。

should do it. The original matches anything that consists of exactly one digit.

这篇关于正则表达式检查字符串是否只包含数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆