Replace Square Brackets for links on web page

前端 未结 2 492
说谎
说谎 2021-01-27 10:13

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:

相关标签:
2条回答
  • 2021-01-27 10:38
    $("div").html(function(i, html) {
        return html.replace(/\[\[(.+?)\]\]/g, "<a href='/dictionary#$1'>$1</a>");
    });
    

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

    0 讨论(0)
  • 2021-01-27 10:48

    Something like:

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

    should work.

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

    0 讨论(0)
提交回复
热议问题