如何用正则表达式把
[url=http://www.baidu.com]www.baidu.com[/url]
替换为
<a href= "http://www.baidu.com " target= "_blank "> www.baidu.com </a>
------解决方案--------------------------------------------------------
string str = @ "[url=http://www.baidu.com]www.baidu.com[/url] "; System.Text.RegularExpressions.Regex reg = new Regex(@ "\[url=([^\]]*?)\]([^\[]*?)\[/url\] ", RegexOptions.IgnoreCase | RegexOptions.Multiline); str = reg.Replace(str, " <a href=\ "$1\ " target=\ "_blank\ "> $2 </a> "); Response.Write(str);
------解决方案--------------------------------------------------------
string url = "[url=http://www.baidu.com]www.baidu.com[/url] ";
Regex reg = new Regex(@ "\[url=(\S*)\](\S*)\[\/url\] ");
//or Regex reg = new Regex(@ "\[url=(.*)\](.*)\[\/url\] ");
url = reg.Replace(url, " <a href=\ "$1\ " target=\ "_blank\ "> $2 </a> ");