javascriptregexparsingbbcode

Simple JavaScript regex to parse tags


I have a simple regex:

\[quote\](.*?)\[\/quote\]

Which will replace [quote] by table, tr and td. (and [/quote] by /td, /tr and /table)

It works perfectly for multiple separate quotes in the same string:

IE:

[quote]
Person 1
[/quote]
Person 3 talking about a quote

[quote]
Person 2
[/quote]
Person 3 talking about another quote.

But when it tries to replace multiple (non-separate) quote in the same string:

IE:

[quote]
[quote]
Person 1
[/quote]
Person 2 quoting person 1
[/quote]
Person 3 quoting person 2 and 1

It messes up, (matches the first quote to the first /quote when it should be matching second quote to first /quote and first quote to last /quote)

How would I edit the regex so it works in both examples?


Solution

  • Regex isn't a good choice for parsing nested structured text. See this question for JavaScript BBCode parser