Skip to content

Instantly share code, notes, and snippets.

@andreafalzetti
Last active October 21, 2016 12:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreafalzetti/2d90bc57dcdcaf1fde4dedd9361f1f93 to your computer and use it in GitHub Desktop.
Save andreafalzetti/2d90bc57dcdcaf1fde4dedd9361f1f93 to your computer and use it in GitHub Desktop.
Randomize variables in a string
Conversation.prototype.randomizeMessage = function(message) {
const pattern = /\[(.*?)\]/g;
const matches = message.match(pattern);
if(matches) {
matches.map((match) => {
const items = match.substring(1, match.length-1).split('|');
const randomItem = items[Math.floor(Math.random()*items.length)];
message = message.replace(match, randomItem);
});
}
return message;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment