JSFiddle - React, Tailwind, and code Playground

by Alexandre Simoes

JavaScript

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

function zap(str) {
  return str.replace(/(y=[^&]+)/g, '') // remove the expression
    .replace(/&+/, '&') // replace any double & resulting from the previous operation
    .replace(/(&$)/g, ''); // remove the eventual final &
}