开发者

regex for .aspx? to modify query string

开发者 https://www.devze.com 2023-02-06 07:32 出处:网络
I need to modify the query string, from:http://server/test/default.aspx to:http://server/test/default.aspx?videoplay=20

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 开发者_如何学Goproblem - I cannot find the correct regex. Can anyone provide a better approach or provide a regex for ".aspx?"


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.


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号