Javascript BBcode function not working

Posted by Dave on Stack Overflow See other posts from Stack Overflow or by Dave
Published on 2013-07-01T03:32:29Z Indexed on 2013/07/01 4:21 UTC
Read the original article Hit count: 139

Filed under:

I have a string I want to convert to divs but it doesn't close the div properly.

The example string i am using is this:

[quote]Quote by: user1 [quote]Quote by: user2 ads[/quote]Test[/quote]Testing 2.

This results in:

<div class="quote" style="margin-left:10px;margin-top:10px;">
  Quote by: user1 
  [quote]Quote by: user2 ads
</div>
  Test[/quote]Testing 2.

But it will not convert the internal quotes properly.

My Javascript function is like this:

function bbcode_parser(str) {
search = new Array(
  /\[b\](.*?)\[\/b\]/g,  
  /\[i\](.*?)\[\/i\]/g,
  /\[quote](.*?)\[\/quote\]/g,
  /\[\*\]\s?(.*?)\n/g);

replace = new Array(
      "<strong>$1</strong>",
      "<em>$1</em>",
      "<div class='quote' style='margin-left:10px;margin-top:10px;'>$1</div>");

for (i = 0; i < search.length; i++) {
    str = str.replace(search[i], replace[i]);
}

return str;
}

I have provided a JSFiddle for you to see it in action: http://jsfiddle.net/gRaFW/2/

Please help :)

© Stack Overflow or respective owner

Related posts about JavaScript