在网上看到一篇文章《在ASP.NET中自动给URL地址加上超链接》:
-----------------------------------------
 作为一个程序员,在完成设计后还要根据程序的情况以及用户的反映不断对程序进行改进,这样才能不断地完善自己的作品。我在制作完软件商务网 http://www.bizsofts.com 的论坛后,发现人们总喜欢在帖子中加上各种有用的URL链接或Email地址。而我当初设计时没有考虑到这一点,使得这些URL链接或Email地址只能以文字的形式而并不是以超链接的形式显示,其它浏览帖子的人还必须把这些URL链接拷贝到浏览器中或把Email地址拷贝到Outlook中才能转到相应的链接地址或发送电子邮件到相应的Email地址。
    发现这个问题后,我就着手进行解决。首先是从网上查找有关这方面的现在代码,可惜的是,在搜索引擎上反复查找也没有发现这方面的文章。后来我一想,干脆我自己用ASP.NET编写一个。
    要想自动显示超链接的关键在于如何能正确识别超链接,毫无疑问的,最有效的方法是用正则表达式。正则表达式是由普通字符(例如字符 a 到 z)以及特殊字符(称为元字符)组成的文字模式,描述了一种字符串匹配的模式,可以用来检查一个串是否含有某种子串、将匹配的子串做替换或者从某个串中取出符合某个条件的子串等。.NET基础类库中包含有一个名字空间和一系列可以充分发挥规则表达式威力的类,用它就可以自动探测出文字中的URL链接或Email地址。下面我具体讲讲如何用ASP.NET(C#)一步步实现我们的目的:首先,要想在ASP.NET(C#)中使用正则表达式就必须把 System.Text.RegularExpressions 这个命名空间包含进来:using System.Text.RegularExpressions;第二步是用正则表达式识别URL超链接:Regex urlregex = new Regex(@"(http:\/\/([\w.]+\/?)\S*)",
   RegexOptions.IgnoreCaseRegexOptions.Compiled);这里的代码是用正则表达式识别Email地址:Regex emailregex = new Regex(@"([a-zA-Z_0-9.-]+\@[a-zA-Z_0-9.-]+\.\w+)",
   RegexOptions.IgnoreCaseRegexOptions.Compiled);第三步,当程序已经识别出URL超链接或Email地址后,必须用<a href=...>超链接</a>对这些超链接进行替换,这样才能把这些文字显示为链接的形式。我这里把它们全部包含在函数中:private void Button1_Click(object sender, System.EventArgs e)
{
   string strContent = InputTextBox.Text;
   Regex urlregex = new Regex(@"(http:\/\/([\w.]+\/?)\S*)",
                    RegexOptions.IgnoreCase RegexOptions.Compiled);
   strContent = urlregex.Replace(strContent,
                "<a href=\"\" target=\"_blank\"></a>");
   Regex emailregex = new Regex(@"([a-zA-Z_0-9.-]+\@[a-zA-Z_0-9.-]+\.\w+)",
      RegexOptions.IgnoreCase RegexOptions.Compiled);
   strContent = emailregex.Replace(strContent, "<a href=mailto:></a>");
   lbContent.Text += "<br>"+strContent;
}通过以上几步,你就可以在网页上自动显示超链接以及Email地址了。欢迎大家下载本示例的源代码,以及到http://www.bizsofts.com 的论坛上观看实际效果。(作者注:该篇文章的英文版已经发表在CodePoject和CodeGuru上)
------------------------------------------
我的疑问是,上面的例程是将;http://www.ynzsjm.com 转换成了<a href" target="_blank"></a>啊,而不是<a href="http://www.ynzsjm.com" target="_blank">http://www.ynzsjm.com</a>?如何才能真正实现给URL地址加上超链接的效果?

解决方案 »

  1.   

    试验了果然和楼主说的一样,改了一下,测试还可以:
      protected void Button1_Click(object sender, EventArgs e)
        {
            string strContent = InputTextBox.Text;
            Regex urlregex = new Regex(@"(http:\/\/([\w.]+\/?)\S*)",
                             RegexOptions.IgnoreCase | RegexOptions.Compiled);
            strContent = urlregex.Replace(strContent,
                         "<a href=\"" + strContent + "\" target=\"_blank\"></a>");
            Regex emailregex = new Regex(@"([a-zA-Z_0-9.-]+\@[a-zA-Z_0-9.-]+\.\w+)",
               RegexOptions.IgnoreCase | RegexOptions.Compiled);
            strContent = emailregex.Replace(strContent, "<a href=mailto:"+ strContent +"></a>");
            lbContent.Text += "<br>" + strContent;    }
      

  2.   

    上面代码还有点问题,生成了<a href="http://www.ynzsjm.com" target="_blank"></a>这样就行了,可以生成<a href="http://www.ynzsjm.com" target="_blank">http://www.ynzsjm.com</a>了。 <asp:TextBox ID="InputTextBox" runat="server"></asp:TextBox><br />
            <br />
            <br />
            &nbsp;<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
              <asp:Literal ID="lbContent" runat="server" ></asp:Literal>//using System.Text.RegularExpressions; //引入命名空间
     protected void Button1_Click(object sender, EventArgs e)
        {
            string strContent = InputTextBox.Text;
            Regex urlregex = new Regex(@"(http:\/\/([\w.]+\/?)\S*)",
                             RegexOptions.IgnoreCase | RegexOptions.Compiled);
            strContent = urlregex.Replace(strContent,
                         "<a href=\"" + strContent + "\" target=\"_blank\">" + strContent + "</a>");
            Regex emailregex = new Regex(@"([a-zA-Z_0-9.-]+\@[a-zA-Z_0-9.-]+\.\w+)",
               RegexOptions.IgnoreCase | RegexOptions.Compiled);
            strContent = emailregex.Replace(strContent, "<a href=mailto:"+ strContent +">" + strContent + "</a>");
            lbContent.Text += "<br>" + strContent;    }
      

  3.   

    可是通常的输入并不是只是一个URL:
    http://www.yuneach.com而可能是:
    请大家看我的网站:http://www.yuneach.com如何把“请大家看我的网站:http://www.yuneach.com”变成:
    “请大家看我的网站:<a href="http://www.yuneach.com">http://www.yuneach.com</a>”?请高手继续指点?
      

  4.   

    gdjlc:
    如果按您的程序,对于Textbox中的内容:
    如果是“请大家看我的网站:http://www.yuneach.com”
    结果是“<a href="http://www.yuneach.com">请大家看我的网站:http://www.yuneach.com</a>,并不太好,
    如果一段话中有多个URL,那又怎么办呢?
      

  5.   

    自己考虑不周全,找到了作者的源代码,改为之后就可以了.
    http://www.vckbase.com/document/viewdoc/?id=1646   <asp:TextBox ID="InputTextBox" runat="server"></asp:TextBox><br />  <br />  <br />  &nbsp;<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />  <asp:Literal ID="lbContent" runat="server" ></asp:Literal>
     protected void Button1_Click(object sender, EventArgs e)
        {
            string strContent = InputTextBox.Text;
            Regex urlregex = new Regex(@"(http:\/\/([\w.]+\/?)\S*)", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.IgnorePatternWhitespace | RegexOptions.Compiled);
            strContent = urlregex.Replace(strContent, "<a href=\"$1\" target=\"_blank\">$1</a>");
            Regex emailregex = new Regex(@"([a-zA-Z_0-9.-]+\@[a-zA-Z_0-9.-]+\.\w+)", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.IgnorePatternWhitespace | RegexOptions.Compiled);
            strContent = emailregex.Replace(strContent, "<a href=mailto:$1>$1</a>");
            lbContent.Text += "<br>" + strContent;    }