1

I am new to Regex and my question is how can i include the ID from the url and remove it using Regex? because as of now , It only removes the actionMe=reload&Id= but the Id still return so after removing it and replacing with new Url, the old id is still included plus the new ID,

Example, Before removing and replacing the Url:

http://localhost:2216/Main/WorkerPage?workerId=10&actionMe=reload&Id=15

And After Removing and replacing the url , it goes like this:

http://localhost:2216/Main/WorkerPage?workerId=10&actionMe=reload&Id=1615

This is my Code Snippet:

var sss = $("#Id").val();
if (window.location.href.indexOf("&actionMe=reload&Id=") > -1) {
                    var regex = /(\&|&)actionMe=reload&Id=/;
                    var location = window.location.href;
                    if (regex.test(location)) {
                        window.location = location.replace(regex, "&actionMe=reload&Id=" + sss)
                    }

                }

Thanks for Answering guys:)

Enrique Gil
  • 754
  • 4
  • 19
  • 50

2 Answers2

2

you can use this code to update url parameters

function updateQueryStringParameter(uri, key, value) {
  var re = new RegExp("([?&])" + key + "=.*?(&|$)", "i");
  var separator = uri.indexOf('?') !== -1 ? "&" : "?";
  if (uri.match(re)) {
    return uri.replace(re, '$1' + key + "=" + value + '$2');
  }
  else {
    return uri + separator + key + "=" + value;
  }
}

I got it here

Now, in your case, you can use it like this

var url = window.location.href;
var sss = $("#Id").val();
var newUrl = updateQueryStringParameter(url, "id", sss);
//do whatever you want to newUrl
//to redirect to new url
window.location = newUrl;
Community
  • 1
  • 1
yesterdaysfoe
  • 768
  • 3
  • 7
  • 23
1

Pretty sure all you need is /&Id=\d+/ as your RegExp. Don't need to select any of actionMe=reload unless you need that for specification (in that case, just add it back). The rest of your code works as intended, just your regex not selecting the precise part you were wanting.

Explanation:

The (\&|&) part of your regex is redundant, as & does not need to be escaped to work. As a matter of fact, since it's in parenthesis, you would end up capturing that & character, if you REALLY need that part, try (?:\&|&) to ignore the capture group. Your code replaced the matched regex, but did not include the number "15" after Id=, which is why it appended 15 after your edited version due to it not being matched and therefore not being replaced. Adding \d+ will select any trailing digits. This should give you the result you wanted.

Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
Izzy
  • 272
  • 1
  • 14
  • hmmm, i think i miss that part in my regex `(\&|&)` but i need to ignore the `?` and starts removing the url from the start of an `&` sign – Enrique Gil Jul 29 '15 at 03:12
  • Try this: `/&.*Id=\d+/` This will select `&actionMe=reload&Id=15` from the URL. Not sure if that's the exact part you want to select, but if not let me know and I can post an alternative with the exact selection you're looking for :) – Izzy Jul 29 '15 at 03:20