[英]Test if string contains only characters
The goal is to compose a regex using the.test() or.match() to check if a string contains all numbers.目标是使用 .test() 或 .match() 组成一个正则表达式来检查字符串是否包含所有数字。
For example:例如:
So far my regex looks like this:到目前为止,我的正则表达式如下所示:
str.match("^[0-9]*$")
I'm getting null returned我收到 null 返回
I am also looking for ways to do this using the.test() method which will return true or false.我也在寻找使用 .test() 方法执行此操作的方法,该方法将返回 true 或 false。
Any help with working with regex is greatly appreciated.非常感谢使用正则表达式的任何帮助。
Your regexp is correct.您的正则表达式是正确的。 Using regexp literal you will be able to use test:
使用正则表达式文字,您将能够使用测试:
/^[0-9]*$/.test('10001')
-> true
/^[0-9]*$/.test('10ab001')
-> false
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.