开发者

need to modify this Regex to not remove a character

I have the following regexp (/\?(.*?)\&/) which when I use it in the following javascript code it removes the "?" from the replacement result.

href=href.replace((/\?(.*?)\&/)开发者_开发百科,"")

The beginning href value is this...

/ShoppingCart.asp?ProductCode=238HOSE&CouponCode=test

I get this as my result right now...

/ShoppingCart.aspCouponCode=test

I would like to get this...

/ShoppingCart.asp?CouponCode=test

How would I modify the Regexp to do this

Thanks for you help.


Put a question mark in the replacement substring:

href=href.replace((/\?(.*?)\&/),"?")

If, say, the character can be something else than a question mark as well (say maybe a slash is a possibility), and you need to preserve which one it is, you can use a capturing group:

href=href.replace((/([?\/])(.*?)\&/),"$1")

Lookbehinds are not supported in JavaScript regexes.


To do it properly, you'll need a regex lookbehind, however this should work in your case:

href=href.replace((/\?(.*?)\&/),"?")
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜