简体   繁体   中英

Regular expression convert url to hyperlink

I've googled some code that converts an url into a hyperlink using bbcode The code is :

// format the url tags: [url=www.website.com]my site[/url]
// becomes: <a href="www.website.com">my site</a>
exp = new Regex(@"\[url\=([^\]]+)\]([^\]]+)\[/url\]");
str = exp.Replace(str, "<a href=\"$1\">$2</a>");

// format the img tags: [img]www.website.com/img/image.jpeg[/img]
// becomes: <img src="www.website.com/img/image.jpeg" />
exp = new Regex(@"\[img\]([^\]]+)\[/img\]");
str = exp.Replace(str, "$1\" />");

I also want to convert ordinary links hyperlinks.I've googled some more and got this:

exp = new Regex("(http://[^ ]+)");
str = exp.Replace(str, "<a href=\"$1\">$1</a>");

The problem is, when i mix them and third regular expression is executed, the first two will be messed up. as it could result in :

<img src="<a href='www.website.com/img/image.jpeg>www.website.com/img/image.jpeg</a>" />

how can i specify in my third regular expression that he cannot convert strings that begin with 'href="' or 'src="' ?

Given the interesting combinations of tags users could throw at you, regular expressions quickly become cumbersome and difficult to use for parsing tags.

BBCode is essentially a grammar unto itself, and the best way to interpret a grammar programmatically is with an actual parser.

Have a look at http://bbcode.codeplex.com/ . I can't vouch for its effectiveness, but they've implemented a parser for BBCode in C# that might do what you're looking for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM