-4

i have a url like this

test.html?dir=asc&end_date=2016-09-23&order=created_at&start_date=2016-08-14

i want to remove the parameter using the following javascript

function removeParam(uri) {

   uri =  uri.replace(/([&\?]start_date=*$|start_date=*&|[?&]start_date=(?=#))/, '');

   return uri.replace(/([&\?]end_date=*$|end_date=*&|[?&]end_date=(?=#))/, '');
}

but it didn't work, anyone know what's wrong with that?

hkguile
  • 4,235
  • 17
  • 68
  • 139

2 Answers2

0

in modern browsers you can do this quite simply

var x = new URL(location.origin + '/test.html?dir=asc&end_date=2016-09-23&order=created_at&start_date=2016-08-14');
x.searchParams.delete('start_date');
x.searchParams.delete('end_date');
var uri = x.pathname.substr(1) + x.search; // substr(1) because you don't have a leading / in your original uri

at least, I think it's simpler

Jaromanda X
  • 53,868
  • 5
  • 73
  • 87
0

Your RegExp is no match!

If you want remove end_date, you should:

uri.replace(/(end_date=)([^*&]+)/, 'end_date=')

And so on.

Fry
  • 1