Javascript BBCode Parser recognizes only the first list item

I have a very simple Javascript BBCode Parser for client side previews (don't want to use Ajax for this). The problem ist, this parser recognizes only the first element of the list:

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

replace = new Array(
      "<strong>$1</strong>",
      "<em>$1</em>",
      "<img src=\"$1\" alt=\"An image\">",
      "<a href=\"$1\">$2</a>",
      "<blockquote>$1</blockquote>",
      "<ol>$2</ol>",
      "<ul>$1</ul>",
      "<li>$1</li>");

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

return str;}

[list]
[*] adfasdfdf
[*] asdfadsf
[*] asdfadss
[/ list]

only the first element is converted to an HTML list element, the rest remains as BBCode:

adfasdfdf [*] asdfadsf
[*] asdfadss

I tried playing with "\ s", "\ S" and "\ n", but mostly I use PHP Regex and brand new for Javascript Regex. Any suggestions?

+3
2

g:

  /\[b\](.*?)\[\/b\]/g,  
  /\[i\](.*?)\[\/i\]/g,
  /\[img\](.*?)\[\/img\]/g,
  /\[url\="?(.*?)"?\](.*?)\[\/url\]/g,
  /\[quote](.*?)\[\/quote\]/g,
  /\[list\=(.*?)\](.*?)\[\/list\]/gi,
  /\[list\]([\s\S]*?)\[\/list\]/gi,
  /\[\*\]\s?(.*?)\n/g);
+4

g m /<regex>/gm .

+1

Source: https://habr.com/ru/post/1743504/


All Articles