Hey everyone, I’m hoping someone can help me out with a Zapier issue I’m facing. I’m trying to count how many times a certain word shows up in a string, but I’m not sure how to do it with Zapier’s code feature.
Here’s what I’m working with:
[{Name:Alex,Surname:Johnson},{Name:Emma,Surname:Taylor},{Name:Chris,Surname:Brown},{Name:Lucy,Surname:Wilson}]
I want to count how many times ‘Name’ appears. In this case, it should be 4.
I’ve cleaned up the string using Zapier’s formatter, but I’m stuck on the counting part. I’ve looked at some word-counting code examples, but I can’t get them to work in Zapier.
If anyone could share a simple JavaScript or Python snippet I could use in Zapier’s code action, I’d really appreciate it. I’m not a coder, so something straightforward would be super helpful.
Thanks in advance for any tips or solutions!
I’ve tackled a similar challenge in Zapier before. Here’s a Python snippet that should do the trick:
input_string = input_data[‘your_field_name’]
word_count = input_string.count(‘Name:’)
This code assumes your string is stored in a field called ‘your_field_name’ in the input data. Just replace that with the actual field name you’re using in Zapier.
The count() method is straightforward and efficient for this task. It’ll return exactly what you need without any complex regex or loops.
Remember to set the output of your Python step to the ‘word_count’ variable so you can use it in subsequent steps of your Zap. Let me know if you need any clarification on implementing this!
hey john, i can help ya out! for counting ‘Name’ in that string, try this JS snippet in Zapier:
const count = (inputString.match(/Name:/g) || ).length;
this’ll give u the count. just replace inputString with ur actual string variable name in Zapier. hope this helps!
Here’s a simple solution using JavaScript that should work well in Zapier’s code action:
const inputString = inputData.your_field_name;
const wordToCount = ‘Name’;
const count = (inputString.match(new RegExp(wordToCount + ‘:’, ‘g’)) || ).length;
return {count: count};
This code creates a dynamic regular expression to match ‘Name:’ and counts all occurrences. Make sure to replace ‘your_field_name’ with the actual field name containing your input string in Zapier.
The benefit of this approach is its flexibility - you can easily change ‘Name’ to any other word you want to count in the future. Also, by returning an object with the count, you can easily reference this result in subsequent Zapier steps.