Skip to main content
Best answer

Get initials from text


paulminors
Forum|alt.badge.img+1

Does anyone know of a way to get the initials from text? 

For example, if I have the text: ‘Paul Minors Zapier Expert’ and I want to return PMZE, how would I do that? I figured you could use a bunch of formatter steps to split the text and return the first letter then all caps everything. But this feels like a long-winded work around. 

Does anyone have any other ideas?

Best answer by ikbelkirasan

@alex This would work for anything like “AlphaTester Inc”.

const companyPO = inputData.companyName
 .split(/([A-Z])/)
 .filter((w) => w.length === 1)
 .join("");
output = [{ companyPO }];

 

View original
Did this topic help you find an answer to your question?
This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

8 replies

AndrewJDavison_Luhhu
Forum|alt.badge.img+10

I bet @ikbelkirasan has some sort of code step that would help here.


Danvers
Forum|alt.badge.img+12
  • Zapier Staff
  • 3731 replies
  • April 7, 2020

Perhaps a Formatter step using Extract Pattern might be able to do the trick here? You’d need to find the Regex for extracting the first letter of each word, but a Google search might be able to help you with that one. 


ikbelkirasan
Forum|alt.badge.img+12
  • Zapier Expert
  • 555 replies
  • April 7, 2020

Hi @paulminors - In a code step and assuming that you have an input property called “text”. The following code should return the initials from it.

const initials = inputData.text
 .split(" ")
 .map((word) => word && word[0].toUpperCase())
 .join("");

output = [{ initials }];

paulminors
Forum|alt.badge.img+1
  • Author
  • Zapier Expert
  • 26 replies
  • April 7, 2020

Brilliant, thanks guys. I’ll let you know once I’ve tried this @ikbelkirasan 


alex
Forum|alt.badge.img+2
  • Zapier Expert
  • 85 replies
  • May 11, 2020

Ay, we actually use the same exact thing to strip down a potential client to a PO Name of sorts. Here’s the code snippet that I have been using for a couple years:

var companyName = inputData.companyName;

var str = companyName;
var matches = str.match(/\b(\w)/g); // ['J','S','O','N']
var companyPO = matches.join(''); // JSON

output = [{companyPO}];

I’d love to add to it, so if the result is only 1 character, to instead use the first 4 characters of the name. For example:

Alpha Testing Corporation = ATC

Google = GOOG

or, a fun challenge is when a company is named something like:

AlphaTester Inc = ATI (taking the AT since they are both capital letters, even though they are the same word).

Any guidance on that would be super!


ikbelkirasan
Forum|alt.badge.img+12
  • Zapier Expert
  • 555 replies
  • Answer
  • May 11, 2020

@alex This would work for anything like “AlphaTester Inc”.

const companyPO = inputData.companyName
 .split(/([A-Z])/)
 .filter((w) => w.length === 1)
 .join("");
output = [{ companyPO }];

 


paulminors
Forum|alt.badge.img+1
  • Author
  • Zapier Expert
  • 26 replies
  • May 12, 2020

Brilliant, thank you both for your help on this!


alex
Forum|alt.badge.img+2
  • Zapier Expert
  • 85 replies
  • May 12, 2020

Thanks @ikbelkirasan - worked perfectly 👌 using the split at any capital letter makes total sense.

I should be able to simply do the other piece of what I mentioned by taking what you did and adding a simple “if less than 1 character is returned, instead capitalize the first 4 letters and use that instead”.

Appreciate your response!