Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save AndriiBozh/fe9bb0c55449f692ab731f508d86d665 to your computer and use it in GitHub Desktop.
Save AndriiBozh/fe9bb0c55449f692ab731f508d86d665 to your computer and use it in GitHub Desktop.
CodeWars: Extract last names of people named Michael
ASSIGNMENT
______________________________________________________
Given a text, for example:
const inputText = "Michael, how are you? - Cool, how is John Williamns and Michael Jordan? I don't know but Michael Johnson is fine. Michael do you still score points with LeBron James, Michael Green AKA Star and Michael Wood?";
get an array of last names of people named Michael. The result should be:
["Jordan", "Johnson", "Green", "Wood"]
Notes:
First name will always be Michael with upper case 'M'.
There will always be a space character between 'Michael' and last name.
Last name will always be one word, starting with an upper-case letter and continuing with lower-case letters.
There will always be at least one 'Micheal' with a valid last name in the input text.
______________________________________________________
SOLUTION
______________________________________________________
function getMichaelLastName(inputText) {
let regEx = /Michael\s[A-Z][a-z]+/g;
let arrFullNames = inputText.match(regEx); //array with filtered results
let secondNames = arrFullNames.map(el => el.replace('Michael ', ''));
return secondNames;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment