如果用户输入了http://aaa.bbb.ccc,下面这个代码将把他的输入转换成http://aaa.bbb.ccc,大家看看正则表达式有多厉害,呵呵。
- <%
- '调用这个函数来显示成超联结
- Response.Writeto_html(s_message)
- %>
- <%
- Functionto_html(s_string)
- to_html=Replace(s_string,"""",""")
- to_html=Replace(to_html,"<","<")
- to_html=Replace(to_html,">",">")
- to_html=Replace(to_html,vbcrlf,"<br>")
- to_html=Replace(to_html,"/<","<")
- to_html=Replace(to_html,"/>",">")
- to_html=edit_hrefs(to_html)
- EndFunction
- %>
- <scriptlanguage="javascript1.2"runat=server>
- functionedit_hrefs(s_html){
- //一个使用正则表达式的典范
- //转换文本中所有的超联结和电子邮件格式
- s_str=newString(s_html);
- s_str=s_str.replace(/bhttp://www(.[w+.:/_]+)/gi,
- "http://¬¤¸$1");
- s_str=s_str.replace(/b(http://w+.[w+.:/_]+)/gi,
- "<ahref="$1">$1</a>");
- s_str=s_str.replace(/b(www.[w+.:/_]+)/gi,
- "<ahref="http://$1">$1</a>");
- s_str=s_str.replace(/bhttp://¬¤¸(.[w+.:/_]+)/gi,
- "<ahref="http://www$1">http://www$1</a>");
- s_str=s_str.replace(/b(w+@[w+.?]*)/gi,
- "<ahref="mailto:$1">$1</a>");
- returns_str;
- }
- </script>