简体   繁体   English

测试字符串是否只包含字符

[英]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:例如:

  1. str "10001" should return true str "10001" 应该返回 true
  2. str "1ab001" should return false str "1ab001" 应该返回 false

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.

相关问题 如何测试仅包含字母字符和破折号(-)的字符串[javascript] - how to test a string that contains only letter characters and dash(-) [javascript] 如果字符串仅包含唯一字符,则使用JavaScript进行正则表达式测试 - Regex test in JavaScript if a string contains only unique characters 如何检查字符串是否仅包含拉丁字符? - How to check if string contains Latin characters only? 验证字符串是否仅包含 javascript 中的某些字符 - Validate if a string contains only some characters in javascript 正则表达式-仅当字符串包含任何其他字符时才允许使用某些字符 - Regex - allow some characters only if the string contains any other characters javascript检查字符串是否仅包含字母数字字符+其他特殊字符 - javascript check if string contains only alphanumeric characters + other special characters 测试字符串是否包含构成另一个字符串的所有字符 - Test If String Contains All Characters That Make Up Another String Javascript正则表达式:测试文本是否仅包含集合中的字符 - Javascript regex: test if text contains only characters from a set 如何检查字符串是否仅包含js中的某些字符? - How to check if a string only contains certain characters in js? 正则表达式检查字符串是否仅包含字母数字字符和空格 - javascript - Regex to check if string contains Alphanumeric Characters and Spaces only - javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM