I have this var
storing a string that represents a URL full of parameters. I'm using AngularJS, and I'm not sure if there is any useful module (or maybe with plain JavaScript) to remove the unneeded URL parameters without having to use regex?
For example I need to remove &month=05
and also &year=2017
from:
var url = "at merge ?derivate=21&gear_type__in=13&engine=73&month=05&year=2017"
Sure you can use RegExr: ((&)year=([^&]))|((&)month=([^&]))
use:
Read more regex :)...
Use the URLSearchParams API:
The advantage of using this API is that it works with and creates strings with correct percent encoding.
For more information, see MDN Developer Reference - URLSearchParams API.
You can use the library https://www.npmjs.com/package/query-string
Convert the params to an object and then just use
delete params.year delete params.month
and convert it back and add it to the original urlUsing string replace:
You can use this function that take 2 parameters: the
param
you are trying to remove and yoursource URL
:Alternative solution with a regex