Link to home
Start Free TrialLog in
Avatar of skij
skijFlag for Canada

asked on

JavaScript/REGEX: replace to matching character OR through end of string

Using JavaScript and regular expressions, how can I replace the section of a string that ends with a specific character OR if that character is not used then to replace through the end of the string?

alert( zap('x=1&y=2&z=3')); // this works
alert( zap('x=1&y=2'));  // this does not work

function zap (str) {
 return str.replace(/y=(.*)\&/g, '');
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Alexandre Simões
Alexandre Simões
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial