如果用户输入了http://aaa.bbb.ccc
下面这个代码将把他的输入转换成http://aaa.bbb.ccc
大家看看正则表达式有多厉害,呵呵。
1
2'调用这个函数来显示成超联结
3Response.Write to_html(s_message)
1
2Function to_html(s_string)
3to_html = Replace(s_string, """", """)
4to_html = Replace(to_html, "<", "<")
5to_html = Replace(to_html, ">", ">")
6to_html = Replace(to_html, vbcrlf, "
<br/>
1")
2to_html = Replace(to_html, "/<", "<")
3to_html = Replace(to_html, "/>", ">")
4to_html = edit_hrefs(to_html)
5End Function
1<script language="javascript1.2" runat="server">
2function edit_hrefs(s_html){
3// 一个使用正则表达式的典范
4// 转换文本中所有的超联结和电子邮件格式
5s_str = new String(s_html);
6
7s_str = s_str.replace(/\bhttp\:\/\/www(\\.[\w+\\.\:\/\\_]+)/gi,
8"http\:\/\/¬¤¸$1");
9
10s_str = s_str.replace(/\b(http\:\/\/\w+\\.[\w+\\.\:\/\\_]+)/gi,
11"<a href=\"$1\">$1<\/a>");
12
13s_str = s_str.replace(/\b(www\\.[\w+\\.\:\/\\_]+)/gi,
14"<a href=\"http://$1\">$1</a>");
15
16s_str = s_str.replace(/\bhttp\:\/\/¬¤¸(\\.[\w+\\.\:\/\\_]+)/gi,
17"<a href=\"http\:\/\/www$1\">http\:\/\/www$1</a>");
18
19s_str = s_str.replace(/\b(\w+@[\w+\\.?]*)/gi,
20"<a href=\"mailto\:$1\">$1</a>");
21
22
23return s_str;
24}
25</script>