0

I use my own created bbcode [wiki] [/wiki] so every word encapsulated between these codes will create a link to my wiki. See code below. This works great.

if (show_wiki_dscn == true ){

    var wiki_start = '<a class="fancywiki" href="http://www.domain.com/support/wiki-inline/';
    var wiki_end = '" data-fancybox-type="iframe"><i class="fa fa-question-circle fa-1x"></i></a>';

    $('div.content_body').each(function() {     
        $(this).html( $(this).html().replace( /\[wiki]/g, wiki_start ).replace( /\[\/wiki]/g, wiki_end));
    })

} else {

    var wiki_start = '<a class="pop-tooltip" href="http://www.domain.com/support/wiki/';
    var wiki_end = '" target="blank" title="Read our wiki"><i class="fa fa-question-circle fa-1x"></i></a>';

    $('div.content_body').each(function() {
        $(this).html( $(this).html().replace( /\[wiki]/g, wiki_start ).replace( /\[\/wiki]/g, wiki_end));
    })
}   

Now my question. I would like to show the wiki "word" instead of the question mark.

So how do I copy [wiki]word[/wiki] and replace <i class="fa fa-question-circle fa-1x"></i> with the "word"

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Gfive
  • 7
  • 6
  • You question would be a lot clearer if you included an example of the input and what you want the output to be. Then use a capturing group in your regex. – Matt Burland Dec 01 '14 at 14:57

1 Answers1

1

You'll need to use a single regex, and capture the word:

$('div.content_body').each(function() {     
    $(this).html( $(this).html().replace( /\[wiki\](.*?)\[\/wiki\]/g, '<a class="fancywiki" href="http://www.domain.com/support/wiki-inline/" data-fancybox-type="iframe">$1</a>'));
})

I use $1 inside the replacement string to insert it.

I also recommend you read Is it really insecure to build HTML strings in Javascript? about your replacement method, as it's currently insecure.

Community
  • 1
  • 1
Scimonster
  • 32,893
  • 9
  • 77
  • 89
  • Perfect. You made my day... Uh insecure, I din't know. How could I make it secure? – Gfive Dec 01 '14 at 15:21
  • Perhaps instead of naively inserting the word, use some more advanced regex stuff to match and then save (`var wikiWord = $(this).html().match(thatRegex)`; then build the HTML using techniques from the other post. – Scimonster Dec 01 '14 at 15:22
  • 1
    That safety should be already present in the bbcode interpreter. So rather than winging it client-side, see if you can formally add `[wikw]...[/wiki]` to the server-side bb script. – Roamer-1888 Dec 01 '14 at 15:35