I am trying to create a script that searches for a pattern in text and wrap a tag around the string it finds.
$(".shop_attributes td").each(function () {
$(this).html(function(i, html) {
return html.replace(/E[0-9]{3,4}/g, "<strong>$1</strong>");
});
});
this is the code i use and it does find what i’m looking but what it actually does is produce a tag with $1 inside. What i expect it to do is to put the string it found into strong tags. What am i doing wrong here?
You need to capture the match, before you can use it. Use parentheses:
Ridiculously over-simplified JS Fiddle demo.