开发者

REGEX assistance with nested pattern?

开发者 https://www.devze.com 2023-04-04 05:00 出处:网络
I have the following string I need to parse: [QUOTE=Mark] [QUOTE=Jack] How are you doing Mark? [/QUOTE] Good to hear from you Jack, Im doing fine!

I have the following string I need to parse:

[QUOTE=Mark]
  [QUOTE=Jack]
    How are you doing Mark?
  [/QUOTE]
 Good to hear from you Jack, Im doing fine!
[/QUOTE]

I am basicly trying to convern this set of BBCode into HTML by converting the [quote] areas into stylized DIVs using the following REGEX

text = text.replace(/\[QUOTE=(开发者_C百科.*?)]([\s\S]*?)\[\/QUOTE\]/gi, '<div class="quotes"><i>Quote by $1</i><br />$2</div>');

This code will properly parse out the first set of QUOTES, but not the nested level quotes. Any ides how I could impprove the expression?


If that's all you're doing, the solution is much simpler:

text = text.replace(/\[QUOTE=(.*?)\]/gi,
                    '<div class="quotes"><i>Quote by $1</i><br />');
text = text.replace(/\[\/QUOTE\]/gi, '</div>');

Your code works too, but you have to apply it multiple times--two in this case, but if there are triply-nested quotes you'll have to make three passes, and so on.


When you get into nested levels you lose the "regular" nature of the input. It becomes more "context free" like HTML which is always a hard spot for regexes.

I suggest you tokenize the string and parse it with somethink like a recursive descent parser.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号