Skip to content

Instantly share code, notes, and snippets.

@danierdev
Last active April 13, 2020 13:57
Show Gist options
  • Save danierdev/556cc34071d23ff11c17ef7cdd3bd3b5 to your computer and use it in GitHub Desktop.
Save danierdev/556cc34071d23ff11c17ef7cdd3bd3b5 to your computer and use it in GitHub Desktop.
Little javascript method to get initials from a name
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