Quantcast
Channel: Use variable to replace all occurrences of specific string - Stack Overflow
Viewing all articles
Browse latest Browse all 2

Use variable to replace all occurrences of specific string

$
0
0

How do I use a dynamic variable as first argument in replace function?

I have this code that searches a user specified string:

 var query = jQuery.trim(jQuery(this).val()).toLowerCase(); console.log(query +' was searched') jQuery('.one-reference').each(function () {    var jQuerythis = jQuery(this);    if (jQuerythis.text().toLowerCase().indexOf(query) === -1) {       jQuerythis.fadeOut();    }    else {       jQuerythis.html(jQuerythis.html().replace(/&/g, '<strong>$&</strong>'));       jQuerythis.fadeIn();    } });

This replace(/&/g, '<strong>$&</strong>')) is not working.

I want to wrap all occurrences of query with <strong> tags.


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images