Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Randomize variables in a string
Conversation.prototype.randomizeMessage = function(message) {
const pattern = /\[(.*?)\]/g;
const matches = message.match(pattern);
if(matches) { => {
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