Reputation: 1083
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
Reputation: 67968
(?:\?|\&)(?:[^=]+)\=(?:[^&\s]+)
Try this.See demo.Replace by empty string
.
https://regex101.com/r/fA6wE2/9
Upvotes: 0
Reputation: 7771
If you want to strip EVERYTHING after the '?', then use something like /?.+/g
.
Upvotes: 0
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.
Upvotes: 1