Last active
April 13, 2020 13:57
-
-
Save danierdev/556cc34071d23ff11c17ef7cdd3bd3b5 to your computer and use it in GitHub Desktop.
Little javascript method to get initials from a name
This file contains hidden or 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
function getInitials(str: string, glue = true): string | string[] { | |
const initials = str.replace(/[^a-zA-Z- ]/g, '').match(/\b\w/g); | |
if (glue) { | |
return initials.join(''); | |
} | |
return initials; | |
} | |
const initials = getInitials('Bon Scott Mitchell'); // AC/DC 🤘 | |
// It will return `BSM`. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment