![]() |
| |||||||
![]() | « Previous Thread | Next Thread » |
| | LinkBack (1) | Thread Tools | Display Modes |
| ||||
| 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: 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
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] (without the spaces in the brackets). 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 language So, if you find an error in the above C# code, let me know and I'll fix it.
__________________ jmurrayhead Did I help you out? Make me popular by clicking the icon!If you found a post helpful, please click the button in the lower right-hand corner of the post.Powered by ASP.Net |
| Sponsored Links |
![]() |
|
| Bookmarks |
| Tags |
| bbcode, function, vb.net |
| Thread Tools | |
| Display Modes | |
|
|
LinkBacks (?)
LinkBack to this Thread: http://www.developerbarn.com/net/40-visual-basic-net-c-bbcode-function.html | ||||
| Posted By | For | Type | Date | |
| ASP.NET Zone - bbCode mit RegEx ersetzen | This thread | Refback | July 16th, 2008 10:01 PM | |
| Sponsored Links |
| ASP.NET Resource Index a directory of ASP.NET tutorials, applications, scripts, assemblies and articles for the novice to professional developer. Free Web Directory Including Chats and Forums Resources, Offer automatic, instant and free directory submissions. | URLZ Web Directory URLZ Web Directory Free Web Directory - Add Your Link The Little Web Directory | Free Web Directory Pegasus free web directory is a free directory organised by categories. Web Directory & SEO Services dirroot web directory |