Skip to content

Instantly share code, notes, and snippets.

@icodesido
Created October 17, 2014 09:31
Show Gist options
  • Save icodesido/b8380ce129ccdb296eac to your computer and use it in GitHub Desktop.
Save icodesido/b8380ce129ccdb296eac to your computer and use it in GitHub Desktop.
Use a regex with placeholders to reverse name and surname
var re = /(\w+)\s(\w+)/;
var str = "John Smith";
var newstr = str.replace(re, "$2, $1");
console.log(newstr);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment