Created
December 24, 2024 07:01
-
-
Save Davis-3450/df3426a574dcb00a3bef6bbc05f98ac2 to your computer and use it in GitHub Desktop.
A JS implementation of nested spintax
This file contains hidden or 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
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
note: nested is broken