0

I'm new to regex and I'm using it in jquery to remove/replace the url but when i triggered an anchortag with href="#" the url will add a # and I'm not able to replace it with my current regex fix to replace the url.

http://localhost:3131/Main/MainPage?a=1221#&ab=&ac=

the original link onload is

http://localhost:3131/Main/MainPage?a=1221

but after triggering an anchortag with href="#" return, it becomes this:

http://localhost:3131/Main/MainPage?a=1221#

and my expected result upon removing the hash(#) in my url is this:

http://localhost:3131/Main/MainPage?a=1221&ab=&ac=

heres my function for replacing the url when closing modal windows in jquery

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

Hope someone can help me as i am a beginner on using Regular expression specially in jquery.

Thanks!

2
  • 6
    Cant you just use uri = uri.replace('#', '');
    – user3559349
    Commented Sep 21, 2015 at 3:03
  • @StephenMuecke let me try that solution! Thanks Commented Sep 21, 2015 at 3:06

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.