首页 > 开发 > 综合 > 正文

利用正则表达式自动给Email地址和Url加上连接

2020-02-03 16:36:05
字体:
来源:转载
供稿:网友

using system;

using system.text.regularexpressions;

 

namespace commlayer

{

       /// <summary>

       /// 自动给邮件地址或email地址加上url

       /// </summary>

       public class hyperlinkurl

       {

              private static regex urlregex = new regex(@"(http:////([/w.]+//?)/s*)",

                     regexoptions.ignorecase|regexoptions.compiled);

              private static regex emailregex = new regex(@"([a-za-z_0-9.-]+/@[a-za-z_0-9.-]+/./w+)",

                     regexoptions.ignorecase|regexoptions.compiled);

             

              public hyperlinkurl()

              {                  

              }

 

              /// <summary>

              /// 生成带连接的字符串

              /// </summary>

              /// <param name="link">需要生成带连接地址的字符串</param>

              /// <returns>经过转换的字符串</returns>

              public static string genhyperlinkurl(string link)

              {

                     link = emailregex.replace(link, "<a href=mailto:$1>$1</a>");

                     link = urlregex.replace(link, "<a href=/"$1/" target=/"_blank/">$1</a>");

 

                     return link;

              }

       }

}

,欢迎访问网页设计爱好者web开发。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表