JAVASCRIPT完全自学手册,中源码的验证修订实例
源代码在线查看: 9-8 ubb代码转换.htm
9-8 UBB代码转换
* { font-size:12px; font-family:宋体, Arial; } /*规定了所有的字体样式*/
body { background-color:buttonface; border-width:0px; margin:0px; padding:5px; }
textarea { border-width:1px; width:200px; height:100px; margin-bottom:5px; }
input { border-width:1px; margin-bottom:5px; }
function ubb2html(){
var str = $("t1").value;
//将UBB代码中的大于号、小于号等转换为HTML实体,以保证安全性
str = str.replace(/&/g,"&").replace(/"/g,""").replace(/ /g," ").replace(/\t/g," ").replace(//g,">").replace(/\r\n/g,"");
//将UBB代码转换为对应的HTML代码
str = str.replace(/\[url\](.+?)\[\/url\]/ig,"$1");
str = str.replace(/\[url=(.+)\](.+?)\[\/url\]/ig,"$2");
str = str.replace(/\[email\](.+?)\[\/email\]/ig,"$1");
str = str.replace(/\[b\](.+?)\[\/b\]/ig,"$1");
str = str.replace(/\[i\](.+?)\[\/i\]/ig,"$1");
str = str.replace(/\[img\](.+?)\[\/img\]/ig,"");
str = str.replace(/\[quote\](.+?)\[\/quote\]/ig,"$1");
str = str.replace(/\[(u|small|h1|h2|h3|h4|h5|h6|strike|blink|sub|sup|del|pre|big)\](.+?)\[\/\1\]/ig,"$2");
str = str.replace(/\[(\#[0-9a-f]{6})\](.+?)\[\/#\]/ig,"$2");
$("t2").value = str;
}
function $(str){ return(document.getElementById(str)); }
UBB代码:
HTML代码: