Adam Výborný
Adam Výborný

Reputation: 731

Remove parameters from url

I really don't understand syntax of .replace :D Can someone please fix my code? Now it removes only &chat=1 from URL, but I need it to remove everything from &chat=1 to end of URL. Sample of URL:

http://xxxxxxx.xx/index.php?menu=1&submenu=1&chat=1&od=3&komu=1

Result what I need:

http://xxxxxxx.xx/index.php?menu=1&submenu=1

Result what is my code doing now:

http://xxxxxxx.xx/index.php?menu=1&submenu=1&od=3&komu=1

Code what I am using now:

location.href=location.href.replace(/&?chat=([^&]$|[^&]*)/i, "");

Thx for help, sorry for my english :)

Upvotes: 1

Views: 100

Answers (1)

Dalorzo
Dalorzo

Reputation: 20014

You are using Regular Expressions as part of your replace and based on what you need this expression seems to be simpler and suits your needs:

&?chat=.*

In Regular Expression

  • ? means optional so the & is optional may or may not exists in the string.
  • chat= this is expected literally on the string followed by
  • .* this basically means anything else from 0 to as many characters exists in the string.

  • i at the end means case insensitve.

Online Demo

Something like this:

"http://xxxxxxx.xx/index.php?menu=1&submenu=1&chat=1&od=3&komu=1".replace(/&?chat=.*/i,"")
//->outputs "http://xxxxxxx.xx/index.php?menu=1&submenu=1"

Upvotes: 2

Related Questions