Ishaan Puniani
Ishaan Puniani

Reputation: 658

regex for .aspx? to modify query string

I need to modify the query string,

from: http://server/test/default.aspx
to: http://server/test/default.aspx?videoplay=20

or

from : http://server/test/default.aspx?toolPlay=1233136844420765
to : http://server/test/default.aspx?videoplay=20&toolPlay=1233136844420765

ie. add videoplay=20& in between the query string

through jQuery. I have tried the .replace function but there is problem - I cannot find the correct regex. Can anyone provide a better approach or provide a regex for ".aspx?"

Upvotes: 1

Views: 198

Answers (2)

ircmaxell
ircmaxell

Reputation: 165261

Well, what I would do is this:

if (url.indexOf('?') != -1) { 
    url += "&videoplay=20";
} else {
    url += "?videoplay=20";
}

It doesn't use a regex, and it handles the case where the url does not contain a query string already.

Upvotes: 2

Oded
Oded

Reputation: 499152

You need to escape both the . and the ? as they are special regex characters:

\.aspx\?

However, you can simply append &videoplay=20 to the end of the URL. The effect will be the same, unless the serverside code is looking at the index of querystring variables (which is very poor practice).

Upvotes: 1

Related Questions