Many people have been using content spinners to spin their content and generate unique articles. I, myself, also used PHP spinner to generate unique content in some of my websites. In this blog post, I am sharing the Javascript version of the spinner.

How to Load AJAX Content with jQuery

 

 

However, this Spintax Parser has been found to work great with simple spintax and may not work good for nested spinning.I would recommend to use the PHP Spintax Code to get better results as expected.

JavaScript Code

var SPINTAX_PATTERN = /\{[^"\r\n\}]*\}/;
var spin = function (spun) {
  var match;
  while (match = spun.match(SPINTAX_PATTERN)) {
   match = match[0];
   var candidates = match.substring(1, match.length - 1).split("|");
   spun = spun.replace(match, candidates[Math.floor(Math.random() * candidates.length)])
  }
  return spun;
 }
var spin_countVariations = function (spun) {
  spun = spun.replace(/[^{|}]+/g, '1');
  spun = spun.replace(/\{/g, '(');
  spun = spun.replace(/\|/g, '+');
  spun = spun.replace(/\}/g, ')');
  spun = spun.replace(/\)\(/g, ')*(');
  spun = spun.replace(/\)1/g, ')*1');
  spun = spun.replace(/1\(/g, '1*(');
  return eval(spun);
 }

Usage

var sample_str="Hi {there|friend|dear}, do you {like|love|hate} using Spintax?";

//alert(spin_countVariations(sample_str)); /* 9 */

alert(spin(sample_str)); /* spin(sample_str) is your parsed text */

There are two parts of this code. One generates the spun text spin(str) and the other is used to get the number of unique variations possible spin_countVariations(str) where str is your string which needs to be spun.

Leave a Reply

Your email address will not be published. Required fields are marked *