当前位置: 首页 > 图文教程 > 网络编程 > ASP > 转换文本为超联和Email格式的代码

ASP
使用ASP中的VB ActiveX.dll文件
用ADO的COMMAND对象实现对WEB数据库动态数据查询的方法
在ASP中使用简单Java类
借助组件使用asp连接informix全方案
用ADSI编程实现IIS中建立虚拟目录
不刷新页面筛选数据库中的数据
使用ISAPI过滤器增强IIS的功能
利用J2ME与ASP建立数据库连接
用通ASP直接获取用户真实IP地址
用ASP编写计数器的优化方法
通过ASP远程注册自己的组件
检测IP地址是否真正合法的函数
用asp做access的远程接口
错误80004005信息处理方法
ASP学习:urldecode 方法补遗
在HTML页面中实现点击数统计
ASP字数计算函数
用ASP随机生成文件名的函数
单页面判断浏览器是否接受 Cookies
存储过程介绍及asp+存储过程的使用

ASP 中的 转换文本为超联和Email格式的代码


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 45 ::
收藏到网摘: n/a

  如果用户输入了http://aaa.bbb.ccc
下面这个代码将把他的输入转换成http://aaa.bbb.ccc
大家看看正则表达式有多厉害,呵呵。

<%
    '调用这个函数来显示成超联结
    Response.Write to_html(s_message)
%>


<%
Function to_html(s_string)
    to_html = Replace(s_string, """", "&quot;")
    to_html = Replace(to_html, "<", "&lt;")
    to_html = Replace(to_html, ">", "&gt;")
    to_html = Replace(to_html, vbcrlf, "<br>")
    to_html = Replace(to_html, "/&lt;", "<")
    to_html = Replace(to_html, "/&gt;", ">")
    to_html = edit_hrefs(to_html)
End Function
%>

<script language="javascript1.2" runat=server>
function edit_hrefs(s_html){
    // 一个使用正则表达式的典范
    // 转换文本中所有的超联结和电子邮件格式
    s_str = new String(s_html);

    s_str = s_str.replace(/\bhttp\:\/\/www(\.[\w+\.\:\/\_]+)/gi,
        "http\:\/\/&not;¤&cedil;$1");

    s_str = s_str.replace(/\b(http\:\/\/\w+\.[\w+\.\:\/\_]+)/gi,
        "<a href=\"$1\">$1<\/a>");
        
    s_str = s_str.replace(/\b(www\.[\w+\.\:\/\_]+)/gi,
        "<a href=\"http://$1\">$1</a>");
        
    s_str = s_str.replace(/\bhttp\:\/\/&not;¤&cedil;(\.[\w+\.\:\/\_]+)/gi,
        "<a href=\"http\:\/\/www$1\">http\:\/\/www$1</a>");
        
    s_str = s_str.replace(/\b(\w+@[\w+\.?]*)/gi,
        "<a href=\"mailto\:$1\">$1</a>");
        
    
    return s_str;
}
</script>