Skip to main content

Ever wanted to split a full name into first name and last name?

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…

  1. Multiple First Names
  2. Multiple Last Names
  3. Middle Name(s)
  4. Name Prefix (e.g. Dr.)
  5. 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

 

@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…

  1. Multiple First Names
  2. Multiple Last Names
  3. Middle Name(s)
  4. Name Prefix (e.g. Dr.)
  5. 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.

thank you so much for your help already


Hi @Ak-tester 

Try asking ChatGPT by OpenAI for help writing the Python code: https://chat.openai.com/

 

 


Hi Troy,

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 ""

outputt"first_name"] = first_name
outputt"middle_name"] = middle_name
outputt"last_name"] = last_name

print(output)

 


Reply