Skip to content Skip to sidebar Skip to footer

Replace Square Brackets For Links On Web Page

I'm trying to do a parse of html content using jQuery/Javascript. I want to look for words between square brackets and change the whole word for a link. Example:
Th

Solution 1:

$("div").html(function(i, html) {
    return html.replace(/\[\[(.+?)\]\]/g, "<a href='/dictionary#$1'>$1</a>");
});

DEMO: http://jsfiddle.net/y4N6e/


Solution 2:

Something like:

$('div').html($('div').html().replace(/\[\[([^\]]+)\]\]/, '<a href="/dictionary#$1">$1</a>')

should work.

But you should hava a look to _.template method!.


Post a Comment for "Replace Square Brackets For Links On Web Page"