0

i have one problem with this code. it selects the first three keyword. what if i want to select the keyword in random number? currently, it has 7 keywords and it selects the first three. what if i want to select keyword 3,6,7. how can i do the random function here?

(function($) {
   $.fn.replacetext = function (target, replacement, max) {
       var limit = max || -1;


       var $textNodes = this  
           .find("*")
           .andSelf()
           .contents()
           .filter(function () {
           return this.nodeType === 3 && !$(this).parent("a").length;
       });

       $textNodes.each(function (index, element) {
           var $element = $(element);
           var words = $element.text().split(/\b/);
           var matches = 0;
           var text = words.map(function (word, index) {
               if (matches >= limit) {
                   return word;
               }

               if (word.match(target)) {
                   ++matches;
                   return word.replace(target, replacement);
               }

               return word;
           });

           $element.replaceWith(text.join(''));
       });
   };
})(jQuery);


$("p").replacetext(/\bdress\b/gi, "<a href='http://www.google.com'>$&</a>", 3);
John Saunders
  • 160,644
  • 26
  • 247
  • 397
rmdsf
  • 23
  • 7
  • The third parameter of `replacetext` is what you're looking for. – Gabriel Garcia Jan 26 '15 at 02:06
  • your `$textNodes.each` is iterating over the array in sequence starting from the first element. You would want to build a `rand` function around there instead – vol7ron Jan 26 '15 at 02:07
  • @GabrielGarcia no, I think that is a count of how many times to do a replacement, not the order to replace. – vol7ron Jan 26 '15 at 02:08
  • Unlike forum sites, we don't use "Thanks", or "Any help appreciated", or signatures on [so]. See "[Should 'Hi', 'thanks,' taglines, and salutations be removed from posts?](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-removed-from-posts). BTW, it's "Thanks in advance", not "Thanks in advanced". – John Saunders Jan 26 '15 at 17:32
  • btw, this is not even a grammar class, thanks. @JohnSaunders – rmdsf Jan 27 '15 at 01:43
  • I know it's not a grammar class, but most people prefer to be informed when they spell things incorrectly, so that they can spell them correctly next time. – John Saunders Jan 27 '15 at 02:28

1 Answers1

0

You may achieve the following behavior by using the following way:

While you are looping for your text nodes, find the maximum random number in each text node by using following code:

var ranMax = $element.text().match(target).length

Then generate a set of random number between the following range (1 to maximum random number)

var arr = []
while (arr.length < max) {
    var randomnumber = Math.ceil(Math.random() * ranMax)
    var found = false;
    for (var i = 0; i < arr.length; i++) {
        if (arr[i] == randomnumber) {
            found = true;
            break
        }
    }
    if (!found) arr[arr.length] = randomnumber;
}

Check if the match index is in the set of random number, replace the value, else return the original value.

var nth = 0;
value = value.replace(target, function (match, i, original) {
    nth++;
    return jQuery.inArray(nth, arr) > -1 ? 
        match.replace(target, replacement) : 
        match;
});

By following the steps above, you may achieve the expected behavior.

For working example, please refer to the following fiddle: http://jsfiddle.net/zeskysee/14xjw4dr/2/

Hopefully this help :D

Community
  • 1
  • 1
Zesky
  • 524
  • 6
  • 16
  • i just noticed, it works well but i got one error which is "Uncaught TypeError: Cannot read property 'length' of null" why? – rmdsf Jan 27 '15 at 05:31
  • Can you show me your fiddle, so i can help you have a look. – Zesky Jan 27 '15 at 09:07