views:

29

answers:

2

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.

+1  A: 

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

href=href.replace((/\?(.*?)\&/),"?")
Lie Ryan
LOL, wow that was simple, feel silly I couldn't think of that. THX!!!
tchrist
+2  A: 

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.

idealmachine