Bogdan
Bogdan

Reputation: 1083

Regex to match all parameters in an URL

I have a list of URLs with parameters in them and I need to strip them of all the parameters.

Sample data:

https://www.example.com/highlights/on-sale-01304&sa=U&ei=2ca1VJXQOYHnsASG3YGYCg&ved=0CBMQFjAA&usg=AFQjCNHucW7fFrA35qeeJbUlAbsPFQNxLg
https://www.example.com/collections/view-all-01240&sa=U&ei=2ca1VJXQOYHnsASG3YGYCg&ved=0CBgQFjAB&usg=AFQjCNGhZzf7rsslCqgHSngyFAbw_nQmvQ
https://www.example.com/highlights/overstock-and-clearance-01217&sa=U&ei=2ca1VJXQOYHnsASG3YGYCg&ved=0CB0QFjAC&usg=AFQjCNHMfQBA4AP51_ikOGyoGEx-aB0-wQ

I have tried this (\?|\&)([^=]+)\=([^&]+)but it matches only a single parameter at a time and basically need to match everything after the ? sign.

Upvotes: 0

Views: 2449

Answers (3)

vks
vks

Reputation: 67968

(?:\?|\&)(?:[^=]+)\=(?:[^&\s]+)

Try this.See demo.Replace by empty string.

https://regex101.com/r/fA6wE2/9

Upvotes: 0

Ian Hazzard
Ian Hazzard

Reputation: 7771

If you want to strip EVERYTHING after the '?', then use something like /?.+/g.

Upvotes: 0

Avinash Raj
Avinash Raj

Reputation: 174706

You need to include a \n character in all the character classes. Because [^=]+ matches also a newline character.

([?&])([^=\n]+)\=([^&\n]+)

Then replace the matched characters with an empty string.

DEMO

Upvotes: 1

Related Questions