var val='0*0+-1*A1+P12*0'; val=val.replace(/(0\*[^+-]*)|([+-][^+-]*?\*0)/g,''); val=val.replace(/\+-1\*/g,'-'); alert(val);