Skip to content

Instantly share code, notes, and snippets.

@Davis-3450
Created December 24, 2024 07:01
Show Gist options
  • Save Davis-3450/df3426a574dcb00a3bef6bbc05f98ac2 to your computer and use it in GitHub Desktop.
Save Davis-3450/df3426a574dcb00a3bef6bbc05f98ac2 to your computer and use it in GitHub Desktop.
A JS implementation of nested spintax
function processSpintax(text) {
// Define the regex that captures the innermost { ... }
const regex = /\{([^{}]+)\}/;
// Keep going while the text still matches { ... }
while (regex.test(text)) {
text = text.replace(regex, (match, contents) => {
// Split the contents by "|"
const parts = contents.split('|');
// Choose a random part from the array
return parts[Math.floor(Math.random() * parts.length)];
});
}
return text;
}
@Davis-3450
Copy link
Author

note: nested is broken

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment