Last active
October 21, 2016 12:16
-
-
Save andreafalzetti/2d90bc57dcdcaf1fde4dedd9361f1f93 to your computer and use it in GitHub Desktop.
Randomize variables in a string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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