«
正则表达式在UBB论坛中的应用(2)

时间:2008-5-31    作者:Deri    分类: 分享


   <p>  六)UBB正则表达模板实例</p><p>  下面是我写的一个UBB函数,这个函数基本上能使你的论坛成为一个优秀的UBB代码论坛了。当然,通过改进后,你可以得到一个更强大的UBB论坛。</p><p>  <code>Function ReThestr(face,str)<br /> 
    dim re,str<br /> 
    re="\>"<br /> 
    str=checkexp(re,str,"&gt;")<br /> 
    re="\<"<br /> 
    str=checkexp(re,str,"&lt;")<br /> 
    re="\n\r\n/"<br /> 
    str=checkexp(re,str,"<P>")<br /> 
    re=chr(32)<br /> 
    str=checkexp(re,str,"&nbsp;")<br /> 
    re="\r"<br /> 
    str=checkexp(re,str," ")<br /> 
    re="\[img\]((http:(\/\/|\\\\)){1}((\w)+[.]){1,3}(net|com|cn|org|cc|tv)(((\/[\~]*|\\[\~]*)<br />(\w)+)|[.](\w)+)*(\w)+[.]{1}(gif|jpg|png))\[\/img\]" '查找图片地址<br /> 
    str=checkexp(re,str," <img src='$1'> ")<br /> 
    re="\[w\](http:(\/\/|\\\\)((\w)+[.]){1,}(net|com|cn|org|cc|tv)(((\/[\~]*|\\[\~]*)(\w)+)|[.](\w)+)*<br />(((([?](\w)+){1}[=]*))*((\w)+){1}([\&](\w)+[\=](\w)+)*)*)\[\/w\]" '查找帧地址<br /> 
    str=checkexp(re,str,"<iframe width='300' height='300' src='$1'></iframe>")<br /> 
    re="([^('>)])(<br>)*((http|https|ftp):(\/\/|\\\\)((\w)+[.]){1,}(net|com|cn|org|cc|tv|([0-9]{1,3}))(((\/[\~]*|\\[\~]*)(\w)+)|[.](\w)+)*(((([?](\w)+){1}[=]*))*((\w)+){1}([\&](\w)+[\=](\w)+)*)*)" '查找链接地址<br /> 
    str=checkexp(re,str,"$1$2 <a href='$3' target=_blank>$3</a> ")<br /> 
    re="([^(http://|http:\\)])((www|cn)[.](\w)+[.]{1,}(net|com|cn|org|cc)(((\/[\~]*|\\[\~]*)(\w)+)|[.](\w)+)*<br />(((([?](\w)+){1}[=]*))*((\w)+){1}([\&](\w)+[\=](\w)+)*)*)" '查找不以http://开头的地址<br /> 
    str=checkexp(re,str,"$1 <a href='http://$2' target=_blank>$2</a> ")<br /> 
    re="([^(=)])((\w)+[@]{1}((\w)+[.]){1,3}(\w)+)" '查找邮件地址<br /> 
    str=checkexp(re,str," <a href='mailto:$2'>$2</a> ")<br /> 
    re="\[0-F]{6})\]((.)+)\[\/color\]" '替换字体色彩<br /> 
    str=checkexp(re,str,"<font color='$1'>$4</font>")<br /> 
    re="\[size=([0-9]{1})\]((.)+)\[\/size\]" '替换字体大小<br /> 
    str=checkexp(re,str,"<font size='$1'>$2</font>")<br /> 
    re="\((.)+)\[\/font\]" '替换字体<br /> 
    str=checkexp(re,str,"<font face='$1'>$3</font>")<br /> 
    re="(\[b\])(.+)(\[\/b\])" '加粗字体<br /> 
    str=checkexp(re,str,"<b>$2</b>")<br /> 
    re="(\[u\])(.+)(\[\/u\])" '下画线<br /> 
    str=checkexp(re,str,"<u>$2</u>")<br /> 
    re="(\[li\])(.+)(\[\/li\])" '列表<br /> 
    str=checkexp(re,str,"<li>$2</li>")<br /> 
    re="(\[QUOTE\])(.+)(\[\/QUOTE\])" '引用<br /> 
    str=checkexp(re,str,"<BLOCKQUOTE>引用:<HR SIZE=1>$2<HR SIZE=1></BLOCKQUOTE>")<br /> 
    re="\[email=((\w)+[@]{1}((\w)+[.]){1,3}(\w)+)\](.+)(\[\/email\])" '邮件<br /> 
    str=checkexp(re,str,"<a href=mailto:$1>$6</a>")<br /> 
    re="(\[center\])(.+)(\[\/center\])" '居中<br /> 
    str=checkexp(re,str,"<center>$2</center>")<br /> 
    re="fuck"<br /> 
    str=checkexp(re,str,"***")<br /> 
    re="操"<br /> 
    str=checkexp(re,str,"***")<br /> 
    re="sex"<br /> 
    str=checkexp(re,str,"***")<br /> 
    re="TMD"<br /> 
    str=checkexp(re,str,"***")<br /> 
    re="shit"<br /> 
    str=checkexp(re,str,"***")<br /> 
    ReThestr=str<br />end function<br />UBB代码如下:<br />[i] [/i] [u] [/u] [url] [/url] [email=] [/email] [img] [/img] </code><blockquote>引用: <hr /><hr /></blockquote>[li] [/li] [font=impact] [color=Yellow]</p>
<p> </p>

   <p>  测试代码如下:</p><p>  <code>http://cn.yahoo.com<br /><a href="http://aol.com/" target="_blank">aol.com</a><a href="http://192.168.0.1/" target="_blank">192.168.0.1</a><br /><a href="http://www.yahoo.com/" target="_blank">www.yahoo.com</a> how are you[/b]<br /><a href="http://page2000.xiloo.com/~page2000?PageNo=2&action=del" target="_blank">page2000.xiloo.com/~page2000?PageNo=2&action=del</a><br />lucaihui@cmmail.com 大家好http:\\page2000.shit<br /><font color=red>http://test.com</font>http://test</code></p><p>  全部符合预想结果</p><p>  七)ASP正则表达式对像函数如下:</p><p>  <code>Function CheckExp(patrn,strng,tagstr)<br /> 
  Dim regEx,Matches<br /> 
  Set regEx=New RegExp'建立一个新对像<br /> 
  regEx.Pattern=patrn'设置模板<br /> 
  regEx.IgnoreCase=true'搜索是否区分大小写的 true表是不区分 flase表示区分<br /> 
  regEx.Global=True'搜索是否应用于整个字符串<br /> 
  Matches=regEx.replace(strng,tagstr)'匹配并替代字符串<br /> 
  CheckExp=Matches返回函数结果<br />end function</code></p><p>  把上面的两个函数存为一个页面(如ubbcode.asp),这样就组成一个完整的UBB函数了。</p><p>  在你的论坛上加上这个函数,就成了支持UBB代码的论坛了。只要在用时调用这个函数就行了。调用形式如</p><p>  text=ReThestr(text)</p></p>