一个新版本的ubb转化程序
发布时间:2006-10-14 2:58:15   收集提供:gaoqian
这段代码将用户输入的ubb代码转化为html格式,注意,需要Script Engine 5.0的支持(使用了RegExp对象)

注:pattern中使用()将知道regexp记忆搜索到的值,$1是第一个(),其余类推。但$2的语法并不被5.0版本的vbscript.dll所支持,我检查了自己机器上的版本(安装过ie 5.5),发现vbscript.dll的版本为5.50.4629,最后修改日期为12月25日。该版本支持$1之类的语法,这个简单的改进使regexp的功能逐渐与perl的正则表达式靠近

function UBBCode(strContent)

    dim objRegExp
    Set objRegExp=new RegExp
    objRegExp.IgnoreCase =true
    objRegExp.Global=True
    'url
    objRegExp.Pattern="(\[URL\])(http:\/\/\S+?)(\[\/URL\])"
    strContent= objRegExp.Replace(strContent,"<A HREF=""$2""
TARGET=_blank>$2</A>")
    objRegExp.Pattern="(\[URL\])(\S+?)(\[\/URL\])"
    strContent= objRegExp.Replace(strContent,"<A HREF=""http://$2""
TARGET=_blank>$2</A>")
    'email
    objRegExp.Pattern="(\[EMAIL\])(\S+\@\S+?)(\[\/EMAIL\])"
    strContent= objRegExp.Replace(strContent,"<A
HREF=""mailto:$2"">$2</A>")

    objRegExp.Pattern="(\[IMG\])(\S+?)(\[\/IMG\])"
    strContent=objRegExp.Replace(strContent,"<IMG SRC=""$2"">")

    objRegExp.Pattern="(\[QUOTE\])(.+?)(\[\/QUOTE\])"
    strContent=objRegExp.Replace(strContent,"<BLOCKQUOTE><font size=1
face=""Verdana, Arial"">quote:</font><HR>$2<HR></BLOCKQUOTE>")

    objRegExp.Pattern="(\[i\])(.+?)(\[\/i\])"
    strContent=objRegExp.Replace(strContent,"<i>$2</i>")

    objRegExp.Pattern="(\[b\])(.+?)(\[\/b\])"
    strContent=objRegExp.Replace(strContent,"<b>$2</b>")
    set objRegExp=Nothing
    UBBCode=strContent
end function
=-=====================
sub UBBCode {
my $ThePost = shift;
$ThePost =~ s/(\[URL\])(http:\/\/\S+?)(\[\/URL\])/ <A HREF="$2"
TARGET=_blank>$2<\/A> /isg;
$ThePost =~ s/(\[URL\])(\S+?)(\[\/URL\])/ <A HREF="http:\/\/$2"
TARGET=_blank>$2<\/A> /isg;
$ThePost =~ s/(\[EMAIL\])(\S+\@\S+?)(\[\/EMAIL\])/ <A
HREF="mailto:$2">$2<\/A> /isg;
if (($UBBImages eq "ON") && ($OverrideImages ne "yes")) {
$ThePost =~ s/(\[IMG\])(\S+?)(\[\/IMG\])/ <IMG SRC="$2"> /isg;
}

$ThePost =~ s/(\[QUOTE\])(.+?)(\[\/QUOTE\])/ <BLOCKQUOTE><font size="1"
face="Verdana, Arial">quote:<\/font><HR>$2<HR><\/BLOCKQUOTE>/isg;
$ThePost =~ s/(\[i\])(.+?)(\[\/i\])/<i>$2<\/i>/isg;
$ThePost =~ s/(\[b\])(.+?)(\[\/b\])/<b>$2<\/b>/isg;
return ($ThePost);
}

 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50