[英]JS Regex to allow only numbers, semicolon and hyphen
I am building an application which should accept strings only with the following formats: 我正在构建一个应该只接受以下格式的字符串的应用程序:
The java script regex should allow only the above formats & shouldn't accept any other formats or symbols . java脚本正则表达式应该只允许上述格式,不应接受任何其他格式或符号。 Can anyone come up with a regex for this?
任何人都可以为此提出正则表达式吗?
This is the RegExp that you need: /^\\d+((;\\d+)*|-\\d+)?$/
这是你需要的RegExp:
/^\\d+((;\\d+)*|-\\d+)?$/
(;\\d+)*
will check for multiple numbers separated by ";" (;\\d+)*
将检查由“;”分隔的多个数字
-\\d+
will check for a range -\\d+
将检查范围
Try 尝试
^[0-9]+([;-][0-9]+)?$
That should work 这应该工作
[0-9]+
matches 1 or more digits [;-] matches a ;
[0-9]+
匹配1个或多个数字[; - ]匹配a ;
or a -
或者
-
(...)?
is an optional match 是可选的匹配
^
anchors the start and $
anchors the end of the string ^
锚定开始, $
锚定字符串的结尾
Assuming that the number portions you are looking for are 5 digits each time, the following should match what you want. 假设您要查找的数字部分每次都是5位数,则以下内容应与您想要的匹配。
[0-9]{5}((;|-)[0-9]{5}){0,1}
If you need different lengths, you can update the {5} with either another fixed length or a range such as {3,5} for a string of 3 to 5 digits. 如果您需要不同的长度,您可以使用另一个固定长度更新{5}或使用{3,5}等范围更新3到5位数的字符串。 If you want to be able to capture more than two numbers with the speperators listed, you can use
如果您希望能够使用列出的拼写器捕获两个以上的数字,则可以使用
[0-9]{5}((;|-)[0-9]{5})*
^[0-9-;]{0,50}$ ^ [0-9 - ;] {0,50} $
0-9 only accept numbers 0-9只接受数字
-; - ; allow only - and ;
只允许 - 和;
{0,50} allow only 50 chars {0,50}只允许50个字符
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.