这段代码将用户输入的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,"
1<a $2""="" href="" target="_blank">$2</a>
")
objRegExp.Pattern="(\[URL\])(\S+?)(\[/URL\])"
strContent= objRegExp.Replace(strContent,"
1<a $2""="" href="" http:="" target="_blank">$2</a>
")
'email
objRegExp.Pattern="(\[EMAIL\])(\S+@\S+?)(\[/EMAIL\])"
strContent= objRegExp.Replace(strContent,"
1<a href="" mailto:$2""="">$2</a>
")
objRegExp.Pattern="(\[IMG\])(\S+?)(\[/IMG\])"
strContent=objRegExp.Replace(strContent,"
1<img $2""="" src=""/>
")
objRegExp.Pattern="(\[QUOTE\])(.+?)(\[/QUOTE\])"
strContent=objRegExp.Replace(strContent,"
1<blockquote><font arial""="" face="" size="1" verdana,="">quote:</font><hr/>$2<hr/></blockquote>
")
objRegExp.Pattern="(\[i\])(.+?)(\[/i\])"
strContent=objRegExp.Replace(strContent,"
1<i>$2</i>
")
objRegExp.Pattern="(\[b\])(.+?)(\[/b\])"
strContent=objRegExp.Replace(strContent,"
1<b>$2</b>
")
set objRegExp=Nothing
UBBCode=strContent
end function
=-=====================
sub UBBCode {
my $ThePost = shift;
$ThePost =~ s/(\[URL\])(http://\S+?)(\[/URL\])/
1<a href="$2" target="_blank">$2<\/A> /isg;
2$ThePost =~ s/(\\[URL\\])(\S+?)(\\[\/URL\\])/ <a href="http:\/\/$2" target="_blank">$2<\/A> /isg;
3$ThePost =~ s/(\\[EMAIL\\])(\S+\@\S+?)(\\[\/EMAIL\\])/ <a href="mailto:$2">$2<\/A> /isg;
4if (($UBBImages eq "ON") && ($OverrideImages ne "yes")) {
5$ThePost =~ s/(\\[IMG\\])(\S+?)(\\[\/IMG\\])/ <img src="$2"/> /isg;
6}
7
8$ThePost =~ s/(\\[QUOTE\\])(.+?)(\\[\/QUOTE\\])/ <blockquote><font face="Verdana, Arial" size="1">quote:<\/font><hr/>$2<hr/><\/BLOCKQUOTE>/isg;
9$ThePost =~ s/(\\[i\\])(.+?)(\\[\/i\\])/<i>$2<\/i>/isg;
10$ThePost =~ s/(\\[b\\])(.+?)(\\[\/b\\])/<b>$2<\/b>/isg;
11return ($ThePost);
12}</b></i></font></blockquote></a></a></a>