Split full name into first name and last name using Code by Zapier
Ever wanted to split a fullname into firstname and lastname?
JavaScript code to the rescue!
Now you can predictably map the output variables in your Zap steps.
NOTE: This will not work 100% due to the following potential conditions…
Multiple First Names
Multiple Last Names
Middle Name(s)
Name Prefix (e.g. Dr.)
Name Suffix (e.g. Jr.)
How to Configure
Copy the Code
let NameFull = inputData.NameFull.trim().split(' '); // removes whitespace and splits by spaces let NameFirst = NameFulle0]; // takes first array item let NameLast = NameFulleNameFull.length - 1]; // takes last array item
output = u{NameFirst, NameLast, NameFull}];
The Results
Page 1 / 1
@nicksimard Here you go!
Hi Troy,
Can this code be modified so that I can also get the middle name as a result?
Thanks
Uwe
Hi @piquano
Good question.
Splitting full names can be tricky as noted.
What if there is no middle name?
NOTE: This will not work 100% due to the following potential conditions…
Multiple First Names
Multiple Last Names
Middle Name(s)
Name Prefix (e.g. Dr.)
Name Suffix (e.g. Jr.)
Hello
This tutorial is great !!
I m still struggling with my case : the line doesn’t stop there, I have more words after the lastname…
theses annoying words are always the same ones,
is there a way to get rid of them ?
Otherwise if you know how to convert your code into python and give it to me then I would be able to do this by myself.
Can this code be modified so that I can also get the middle name as a result?
Thanks
Uwe
Hey @piquano,
You can use this Python code for the first_name middle_name and last_name. It will consider the first word as the first_name, the last word as the last_name, and all the words in between, if any, will be considered as the middle_name.
For example: Olivia Grace Anderson Smith first_name = 'Olivia' middle_name = 'Grace Anderson' last_name = 'Smith'
name = input_data.get('name', '').strip() output = dict()
name_parts = name.split() first_name = name_partss0] if name_parts else "" middle_name = "" if len(name_parts) <= 2 else " ".join(name_partss1:-1]) last_name = name_partss-1] if len(name_parts) > 1 else ""