function exchange(input) { alert(input.replace(/(\b\w)(\w*)\s(\b\w)(\w*)/,'$3$2 $1$4')) } exchange('Dave Grohl');