I've looked all over the net for something like this for ASP.Net and couldn't find what I needed. After reviewing several pages, I was able to use individual pieces of information to build this simple regular expression to parse BBCode into HTML.
I thought something like this would be really long and complicated, but it's not at all:
VB.Net code:
C# Code:Code:Public Function BBCode(ByVal strTextToReplace As String) As String '//Define regex Dim regExp As Regex '//Regex for URL tag without anchor regExp = New Regex("\[url\]([^\]]+)\[\/url\]") strTextToReplace = regExp.Replace(strTextToReplace, "<a href="http://www.developerbarn.com/asp-net/"$1"">$1</a>") '//Regex for URL with anchor regExp = New Regex("\[url=([^\]]+)\]([^\]]+)\[\/url\]") strTextToReplace = regExp.Replace(strTextToReplace, "<a href="http://www.developerbarn.com/asp-net/"$1"">$2</a>") '//Image regex regExp = New Regex("\[img\]([^\]]+)\[\/img\]") strTextToReplace = regExp.Replace(strTextToReplace, "<img src="http://www.developerbarn.com/asp-net/"$1"" />") '//Bold text regExp = New Regex("\[b\](.+?)\[\/b\]") strTextToReplace = regExp.Replace(strTextToReplace, "<b>$1</b>") '//Italic text regExp = New Regex("\[i\](.+?)\[\/i\]") strTextToReplace = regExp.Replace(strTextToReplace, "<i>$1</i>") '//Underline text regExp = New Regex("\[u\](.+?)\[\/u\]") strTextToReplace = regExp.Replace(strTextToReplace, "<u>$1</u>") '//Font size regExp = New Regex("\[size=([^\]]+)\]([^\]]+)\[\/size\]") strTextToReplace = regExp.Replace(strTextToReplace, "<span style=""font-size: $1px"">$2</span>") '//Font color regExp = New Regex("\[color=([^\]]+)\]([^\]]+)\[\/color\]") strTextToReplace = regExp.Replace(strTextToReplace, "<span style=""color: $1"">$2</span>") Return strTextToReplace End Function
This uses block brackets to define the BBCode. For example, if you wanted to place a URL on your page, you would write it like this:Code:public string BBCode(string strTextToReplace) { ////Define regex Regex regExp; ////Regex for URL tag without anchor regExp = new Regex(@"\[url\]([^\]]+)\[\/url\]"); strTextToReplace = regExp.Replace(strTextToReplace, "<a href=\"$1\">$1</a>"); ////Regex for URL with anchor regExp = new Regex(@"\[url=([^\]]+)\]([^\]]+)\[\/url\]"); strTextToReplace = regExp.Replace(strTextToReplace, "<a href=\"$1\">$2</a>"); ////Image regex regExp = new Regex(@"\[img\]([^\]]+)\[\/img\]"); strTextToReplace = regExp.Replace(strTextToReplace, "<img src=\"$1\" />"); ////Bold text regExp = new Regex(@"\[b\](.+?)\[\/b\]"); strTextToReplace = regExp.Replace(strTextToReplace, "<b>$1</b>"); ////Italic text regExp = new Regex(@"\[i\](.+?)\[\/i\]"); strTextToReplace = regExp.Replace(strTextToReplace, "<i>$1</i>"); ////Underline text regExp = new Regex(@"\[u\](.+?)\[\/u\]"); strTextToReplace = regExp.Replace(strTextToReplace, "<u>$1</u>"); ////Font size regExp = new Regex(@"\[size=([^\]]+)\]([^\]]+)\[\/size\]"); strTextToReplace = regExp.Replace(strTextToReplace, "<span style=\"font-size: $1px\">$2</span>"); ////Font color regExp = new Regex(@"\[color=([^\]]+)\]([^\]]+)\[\/color\]"); strTextToReplace = regExp.Replace(strTextToReplace, "<span style=\"color: $1\">$2</span>"); return strTextToReplace; }
[url=http://www.developerbarn.com]DeveloperBarn Forums[/url].
You can easily adapt it with other BBCode if you understand Regex. I'll add more examples as I get time.
Note: C# isn't my native languageSo, if you find an error in the above C# code, let me know and I'll fix it.



LinkBack URL
About LinkBacks
So, if you find an error in the above C# code, let me know and I'll fix it.
Reply With Quote

Bookmarks