Regex BBCode to HTML

前端 未结 2 1074
误落风尘
误落风尘 2021-01-22 00:17

I writing BBcode converter to html.
Converter should skip unclosed tags.

I thought about 2 options to do it:
1) match all tags in once using one regex call, like

2条回答
  •  独厮守ぢ
    2021-01-22 01:01

    r = new System.Text.RegularExpressions.Regex(@"(?:\[b\])(?(?>\[b\](?)|\[/b\](?<-DEPTH>)|.)+)(?(DEPTH)(?!))(?:\[/b\])", System.Text.RegularExpressions.RegexOptions.Singleline);
    
     var s = r.Replace("asdfasdf[b]test[/b]asdfsadf", "$1");
    

    That should give you only elements that have matched closing tags and also handle multi line (even though i specified the option of SingleLine it actually treats it as a single line)

    It should also handle [b][b][/b] properly by ignoring the first [b].

    As to whether or not this method is better than your first method I couldn't say. But hopefully this will point you in the right direction.

    Code that works with your example below: System.Text.RegularExpressions.Regex r;

    r = new System.Text.RegularExpressions.Regex(@"(?:\[b\])(?(?>\[b\](?)|\[/b\](?<-DEPTH>)|.)+)(?(DEPTH)(?!))(?:\[/b\])", System.Text.RegularExpressions.RegexOptions.Singleline);
    
    var s = r.Replace("[b]bla bla[/b]bla bla[b] " + "\r\n" + "bla bla [/b]", "$1");
    

提交回复
热议问题