Pedro
Pedro

Reputation: 1450

Regular expression to validate last digits

How can I validate the last digits of URL /?d=123

the URL will always ends with /?d=(no more than 4 numbers) ex.12345 will never appear

http://www.test.com/?d=123

This is what i have, but I don't know how to match just the ones that end with 123 and 4321

(http(s)?://)([\w-]+\.)+[\w-]+(/[\w- ;,./?%&=]*)?

Upvotes: 0

Views: 193

Answers (2)

degant
degant

Reputation: 4981

Regex to validate a URL for which the query parameter contains only digits (1 to 4 like OP suggested):

^(http(s)?://)([\w-]+.)+[\w-]+([\w- ;,./%&=]*)\?((\w)+=(\d){1,4})$

Regex to validate a URL for which query parameters are either 123 or 4321:

^(http(s)?://)([\w-]+.)+[\w-]+([\w- ;,./%&=]*)\?((\w)+=(123|4321))$

Regexstorm Demo

EDIT: Minor modifications as per OP's requirements and @Stephen P's suggestions

Upvotes: 2

quirimmo
quirimmo

Reputation: 9988

This is for matching only these values at the end:

/(123|4321)$/

Upvotes: 1

Related Questions