Last active
February 25, 2019 18:21
-
-
Save IOIO72/a8586cb3cda4d8710d0d002800083165 to your computer and use it in GitHub Desktop.
Extract first name out of string of full name
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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