一个新版本的ubb转化程序
文章类别:
asp | 发表日期:2008-10-5 20:41:14
这段代码将用户输入的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);
}