简体   繁体   English

javascript正则表达式问题

[英]javascript regex question

i want to check for certain regex expression to make sure some string is followed by '::' and some number. 我想检查某些正则表达式,以确保某些字符串后跟“ ::”和一些数字。 The number can be between 1 and 999999999999. 该数字可以在1到999999999999之间。

for example: 'ACME LOCK & KEY::42443' should pass where 'ABC Inc.' 例如:“ ACME LOCK&KEY :: 42443”应该通过“ ABC Inc.” should fail. 应该失败。

any help? 有什么帮助吗?

Try this: 尝试这个:

/::[1-9]\d{0,11}$/.test(str)

This will return true for every string that ends with :: followed by an integer between 1 and 999999999999 inclusive. 对于以::结尾的所有字符串,其后为1到999999999999(含)之间的整数,它将返回true。

Here, this will match a string that ends in :: followed by 1-12 digits. 在这里,这将匹配一个以::结尾的字符串,后跟1-12个数字。

/^.+::[1-9]\d{0,11}$/.test(stringToTest)

This also checks to make sure there is a string of at least 1 character prior to the :: 这也进行检查,以确保至少有1个字符之前的字符串::

Tests: 测试:

FAIL: asdf
PASS: asdf::123
FAIL: asdf::
FAIL: asdf::0
PASS: asdf::999999999999
FAIL: asdf::9999999999999
FAIL: ::asdf
FAIL: ::999

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM