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

ASP
用PreRender解决DataGrid分页最后一页行数不满的排版问题
基于ASP的站内多值搜索
XLS与MDB文件格式互换全攻略
一个用ASP生成html的新方法
将指定的asp文件内容生成HTML文件
使用Session记录页面地址和实现页面返回功能
IIS6架设网站常见问题及症状举例答疑
ASP调用WEBSERVICE文档
用Asp获取Dll加密新闻内容
Access2000数据库80万记录通用快速分页类
如何防止ASP木马在服务器上运行
如何使用javascript来写ASP程序
用存储过程实现ASP对数据库访问
学会在ASP中使用存储过程
ASP中和星期有关的自定义函数
水晶报表打印单据时增加空行或空白行的示例脚本
ASP+Access莫名奇妙的sql语句错误解决
ASP获取客户端MAC地址
在ASP中执行Ping命令,并且返回结果
如何使用ASP建立虚拟的FTP服务器

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 48 ::
收藏到网摘: 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>