0

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- ;,./?%&=]*)?
6
  • What is your requirement? You want to ensure that only digits are present in the query parameter? Commented May 2, 2017 at 17:49
  • /^\d+$/.test( url.split('=').pop() ) Commented May 2, 2017 at 17:49
  • if the only thing you're looking for in the url are digits you can use /(\d+)/g to get the numbers back. But this is a very loose regex. Commented May 2, 2017 at 17:50
  • @degant im trying to validate a url that ends(match) with 123 it has to be a valid url Commented May 2, 2017 at 17:54
  • @Pedro so the end of the URL has to be only 123 and nothing else? Commented May 2, 2017 at 17:55

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

6 Comments

OP: ?d=(no more than 4 numbers) ex.12345 will never appear so {1,4}
Valid point @StephenP, thanks! Updated regex to allow only {1,4}
Thank you for you help, a quick question..i wast looking at your refiddle demo, and if we add 4321111, it highlights the link but ignores the last digits that don't match, to ignore the one that don't match
@Pedro that won't be a problem. You can simply add a dollar ($) at the end of the regex pattern to make sure this issue doesn't come in your code. Updated the answer with the same.
@degran I know that I already marked your asnwers as correct(and it is) I running your 'Regextorm' example and if I add the other link to the test area, dont highlight any link? do you know the reason ?
|
1

This is for matching only these values at the end:

/(123|4321)$/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.