Skip to content

Instantly share code, notes, and snippets.

@IOIO72
Last active February 25, 2019 18:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IOIO72/a8586cb3cda4d8710d0d002800083165 to your computer and use it in GitHub Desktop.
Save IOIO72/a8586cb3cda4d8710d0d002800083165 to your computer and use it in GitHub Desktop.
Extract first name out of string of full name
/*
* getFirstName extracts the first name out of a string of the full name.
* Examples (input -> result):
* 'Tim Berners-Lee' -> 'Tim'
* 'Berners-Lee, Tim' -> 'Tim'
* 'Tim' -> 'Tim'
* */
const getFirstName = names => {
const reg = new RegExp('.*, ([a-zA-Z-]*)|([a-zA-Z-]*) .*', 'i');
const result = reg.exec(names);
return (Array.isArray(result) && result.length > 0) ? result[1] || result[2] : names;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment