JSFiddle - React, Tailwind, and code Playground
JavaScript
for(var i = 0; i < 10; i++){
document.body.innerHTML += (replaceWithRandomFromRegexes("You aren't a crackpot! You're a prodigy!", ["(genius|prodigy)", "(freak|loony|crackpot|crank|crazy)", "(You're |You are |Thou art )", "(aren't|ain't|are not)"]))+"<br/>";
}
function replaceWithRandomFromRegexes(theString, theRegexes) {
//alert(theRegexes);
for (var i = 0; i < theRegexes.length; i++) {
theString = globalReplaceWithRandomFromRegex(theString, theRegexes[i]);
//alert(theRegexes[i]);
}
//alert("All the regexes: " + theRegexes);
return theString;
}
function globalReplaceWithRandomFromRegex(theString, theRegexString) {
var theRegex = new RegExp(theRegexString, "gi");
//replace all matches of theRegex with '<thing to replace>'
theString = theString.replace(theRegex, "<thing to replace>")
//replace the first match of '<thing>'
while (theString.indexOf("<thing to replace>") != -1) {
theString = theString.replace("<thing to replace>", getRandomStringFromNestedParentheses(theRegexString));
}
//alert(theString);
return theString;
}
function getRandomStringFromNestedParentheses(theString) {
while (theString.indexOf("(") != -1) {
theString = replaceInFirstParentheses(theString);
}
return theString;
}
function replaceInFirstParentheses(theString) {
//find the index of the first parentheses
var parenthesesIndex = theString.indexOf("(");
var randomString = getRandomStringInsideParentheses(theString, parenthesesIndex);
//alert(randomString);
//theString = theString.replace();
//find the string to replace
var stringToReplace = theString.substring(parenthesesIndex, getCorrespondingParenthesesIndex(theString, parenthesesIndex) + 1);
//alert(stringToReplace);
...