Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save reganstarr/8bb03b92af94bbbeff2b to your computer and use it in GitHub Desktop.
Save reganstarr/8bb03b92af94bbbeff2b to your computer and use it in GitHub Desktop.
/*
EXAMPLE:
input.tweetContent = This is a tweet about Zapier that mentions @zapier twice as well as reganstarr.
*/
var commaSeperatedTwitterUsernames = "Zapier,ReganStarr";
// You'll need to change input.tweetContent to match whatever input mapping you defined in the "Input" section of Zapier. (https://zapier.com/help/code/#how-does-it-work)
var tweetContent = input.tweetContent;
// split the comma-separated string of usernames into an array
var twitterUsernamesArray = commaSeperatedTwitterUsernames.split(',');
// filter out any empty strings
twitterUsernamesArray = twitterUsernamesArray.filter(function(n) {return n.length != 0});
for (var i = 0; i < twitterUsernamesArray.length; i++) {
var twitterUsername = twitterUsernamesArray[i];
// remove any accidental whitespace
twitterUsername = twitterUsername.trim();
// we need to account for any mentions of the username that already include the "@" symbol. Otherwise we might get two "@" sybmols by mistake (@@Zapier.) So first, we're actually going to strip out the "@" symbol from any usernames that were defined in the comma-separated variable.
// create a global, case-insensitive regular expression of the username with the "@" symbol
var regularExpressionOfUsernameWithAtSymbol = new RegExp("@" + twitterUsername,'gi');
// replace all occurences of the regular expression with just the username
tweetContent = tweetContent.replace(regularExpressionOfUsernameWithAtSymbol, twitterUsername);
// Now that we are sure that we won't have any double "@" symbol occurences, like @@Zapier, we can go ahead and replace any mention of the defined usernames with the format of @username
// create a global, case-insensitive regular expression of just the username
var regularExpressionOfUsername = new RegExp(twitterUsername,'gi');
// replace all occurences of the regular expression with the the '@' symbol and the username
tweetContent = tweetContent.replace(regularExpressionOfUsername, '@' + twitterUsername);
}
output = {output: tweetContent};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment