Kung Fu Ninja
Kung Fu Ninja

Reputation: 3752

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.

for example: 'ACME LOCK & KEY::42443' should pass where 'ABC Inc.' should fail.

any help?

Upvotes: 1

Views: 56

Answers (2)

CaffGeek
CaffGeek

Reputation: 22054

Here, this will match a string that ends in :: followed by 1-12 digits.

/^.+::[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 ::

Tests:

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

Upvotes: 2

Gumbo
Gumbo

Reputation: 655239

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.

Upvotes: 4

Related Questions