let name = "Mike Jones"
Desired result —> “M. Jones”
let name = "Mike Jones" // Who? Mike Jones // Create an array containing each of the words in the name var names = name.split(/s+/); // Replaces the first name with an initial, followed by a period. names = names.substr(0, 1) + "."; // Glue the pieces back together. var name_abbr = names.join(' '); console.log(name_abbr);
Or if you want to get fancy, you could use some regex to get the first letter of the first word, and then everything else, and then just put a period between them.
let name = "Mike Jones" // Who? Mike Jones var parts = name.match(/^([a-zA-Z])[^s]*(.*)$/); var abbr_name = parts + "." + parts; console.log(abbr_name);