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

2001-02-12 10:35:42  作者
假如用户输入了http://aaa.bbb.ccc
下面这个代码将把他的输入转换成http://aaa.bbb.ccc
大家看看正则表达式有多厉害,呵呵。

%26lt;%
'调用这个函数来显示成超联结
Response.Write to_html(s_message)
%%26gt;


%26lt;%
Function to_html(s_string)
to_html = Replace(s_string, %26quot;%26quot;%26quot;%26quot;, %26quot;%26amp;quot;%26quot;)
to_html = Replace(to_html, %26quot;%26lt;%26quot;, %26quot;%26amp;lt;%26quot;)
to_html = Replace(to_html, %26quot;%26gt;%26quot;, %26quot;%26amp;gt;%26quot;)
to_html = Replace(to_html, vbcrlf, %26quot;%26lt;br%26gt;%26quot;)
to_html = Replace(to_html, %26quot;/%26amp;lt;%26quot;, %26quot;%26lt;%26quot;)
to_html = Replace(to_html, %26quot;/%26amp;gt;%26quot;, %26quot;%26gt;%26quot;)
to_html = edit_hrefs(to_html)
End Function
%%26gt;

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

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

s_str = s_str.replace(/\b(http\:\/\/\w+\.[\w+\.\:\/\_]+)/gi,
%26quot;%26lt;a href=\%26quot;$1\%26quot;%26gt;$1%26lt;\/a%26gt;%26quot;);

s_str = s_str.replace(/\b(www\.[\w+\.\:\/\_]+)/gi,
%26quot;%26lt;a href=\%26quot;http://$1\%26quot;%26gt;$1%26lt;/a%26gt;%26quot;);

s_str = s_str.replace(/\bhttp\:\/\/%26amp;not;¤%26amp;cedil;(\.[\w+\.\:\/\_]+)/gi,
%26quot;%26lt;a href=\%26quot;http\:\/\/www$1\%26quot;%26gt;http\:\/\/www$1%26lt;/a%26gt;%26quot;);

s_str = s_str.replace(/\b(\w+@[\w+\.?]*)/gi,
%26quot;%26lt;a href=\%26quot;mailto\:$1\%26quot;%26gt;$1%26lt;/a%26gt;%26quot;);


return s_str;
}
%26lt;/script%26gt;