I've successfully converted this php spintax code into javascript, source from http://www.edcharkow.com/blog/spintax-easy-php-code/
But some times the repeated the results are way too often, and I'm thinking to check the last spun spintax with the result, if it's true, it should continue spinning.
Now my problem is, how do I create a new instance off that Spin(s)
function as the last spun result everytime I perform a spin?
I maybe approaching this problem in the most straightforward manner, but would love to hear any better alternative if there's such.
var text = "{{this|that} is {nice|awesome|great}!|What {are you|am I} doing?|I can't believe {this|that} is {happening|so awesome!}}";
alert( Spin(text) );
function Spin(s)
{
var m = s.match(/\{(.*?)\}/i);
if ( !m ) return s;
var t = m[1];
if ( t.indexOf("{") !== false )
{
t = t.substr(t.lastIndexOf("{") + 1);
}
var parts = t.split("|");
var regex = new RegExp("\{" + preg_quote(t) + "\}");
s = s.replace( regex, parts[Math.floor(Math.random() * parts.length)] );
return Spin(s);
}
function preg_quote(str, delimiter)
{
return String(str).replace(new RegExp('[.\\\\+*?\\[\\^\\]$(){}=!<>|:\\' + (delimiter || '') + '-]', 'g'), '\\$&');
}