str=" foo@alpha!test foo!test@alpha "; reg=/((\w+)(!(\w+)|@(\w+)){2})/g b=str.replace(reg,'\n$1 ==> name="$2",atr_exclamation="$4",attr_at="$5"'); alert(b);