Jared L Cowan
Jared L Cowan

Reputation: 56

JS Regex Help to match URL

I need to match only urls C and D.

Given I have these sites:

a.  http://www.website.com/our-resources/news/?p=1
b.  http://www.website.com/example/voice-of-the-customer/news/?p=12
c.  http://www.website.com/our-resources/news/?p=123
d.  http://www.website.com/example/?p=4321
e.  http://www.website.com/example/products/touchchat/news/?p=12345

I was using .*\?((.*=.*)(&?[0-9]{3,4})) on www.regexpal.com and it's matching c, d and e.

Upvotes: 0

Views: 74

Answers (2)

anubhava
anubhava

Reputation: 786091

You can use this regex:

/[^?]*\?.*=\d{3,4}(?:&|$)/

This will only match a URL with query parameter with 3 or 4 digits only (c & d test cases in your question).

RegEx Demo

Upvotes: 1

Aminah Nuraini
Aminah Nuraini

Reputation: 19206

Use this:

.*\?((.*=)([0-9]{3,4}))

It was caused by the second .* in .*=.*

Upvotes: 0

Related Questions