简体   繁体   中英

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- ;,./?%&=]*)?

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))$

Refiddle Demo

Regexstorm Demo

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

这仅用于最后匹配这些值:

/(123|4321)$/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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