0

I am using a slightly modified version of a converting a keyword to a link presented in another question here.

The code, with some omissions, is as following:

$(".keyword_search").each(function() {
    var targetword = 'TEST';
    var explanation = 'Is something you do to find out if stuff works';
    //targetword and explanation actually defined in a loop, but omitted here  
    var content = $(this)[0];
    var re = new RegExp("(\\b"+targetword+"\\b)", "gi");
    content.innerHTML = content.innerHTML.replace(re,keywordconvert);
}

function keywordconvert(str, p1, offset, s ) {
     return '<a href="#" data-toggle="tooltip" title="'+p1+'">'+p1+'</a>';
}

I have few questions about this code.

1) Where are the parameters of keywordconvert defined? str and p1 both contain the string to be replaced, offset contains the starting point of the word in the the content and s contains content.

2) How would I go about adding the explanation string to the title-class of the the link in the keywordconvert function? In other words, how can I add parameters to this function?

Any help would be appreciated, I've been scouring the web for answers for far too long.

  • Have a look here https://developer.mozilla.org/en/docs/JavaScript/Reference/Global_Objects/String/replace#Specifying_a_function_as_a_parameter – Tetaxa Mar 25 '13 at 08:37
  • I think it has something to do with this: http://stackoverflow.com/questions/10855908/how-to-overload-functions-in-javascript – Natrium Mar 25 '13 at 08:37

1 Answers1

2

1) Where are the parameters of keywordconvert defined?

By the String#replace definition. When you call replace with a regular expression as the search and a function as the replace, it calls the function with the matched string, any capture groups defined, and then the offset at which the match occurred, and finally the string itself. Since the regular expression being passed in defines one capture group, that means that str will be the matched substring, p1 will be the content of the capture group, offset will be the index (offset) within the string, and s will be the entire string on which replace was called.

2) How would I go about adding the explanation string to the title-class of the the link in the keywordconvert function?

You'd probably have to create your own function inside your each iterator function, which isn't ideal, e.g.:

$(".keyword_search").each(function() {
    var targetword = 'TEST';
    var explanation = 'Is something you do to find out if stuff works';
    //targetword and explanation actually defined in a loop, but omitted here  
    var content = $(this)[0];
    var re = new RegExp("(\\b"+targetword+"\\b)", "gi");
    content.innerHTML = content.innerHTML.replace(re,function(str, p1, offset, s) {
        return keywordconvert(str, p1 + explanation, offset, s);
    });
});

Note that where I have p1 + explanation I assume you would want to do something more interesting, but it's p1 that sets the title.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875