function exchange(input) { input.replace(/(\b\w)(\w+\b)\s(\b\w)(\w+\b)/g, function (str,p1,p2,p3,p4) { alert(p3+p2+' '+p1+p4); }) } exchange('Dave Grohl');