1

On the following string I want to remove sort=*. I can get this done by run loop over it But want to get it done using regex which is new to me.

So start point should be "sort" and end point should be '&' and sort value could be *

I have created this one but dont how to add end point

string.replace((sort=*)\w,'')



"category_id=249&sort=name&category_filter=15&"

to

"category_id=249&category_filter=15&"
Jitender
  • 7,593
  • 30
  • 104
  • 210
  • 1
    Modern browsers provide [URLSearchParams](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams/URLSearchParams) to work with query strings. – Yury Tarabanko Jun 16 '17 at 07:51
  • 1
    You can build string by chosen properties from something object, it could be more declarative approach than editing existing string. – Arkadiusz Wieczorek Jun 16 '17 at 08:01

1 Answers1

1

You ca use this regex instead (sort=.*?&), which can match every thing from sort= until the first occurrence of &

regex demo

Output

category_id=249&category_filter=15&
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140