简单的Html转换UBB的程序

又是一场大雪过后,天空像海一样蔚蓝,甚至比海更加晶莹剔透。千峰万岭,极目望去,尽是白色,闪耀着一片连接不断的银光。山顶积雪未融,如白银宫网。
这个功能主要是让UBB转贴的时候方便点.1.自动识别图片,超链接,字体颜色,和字体加粗,倾斜,下划线的HTML代码,并且转换为UBB代码.
2.兼容IE和Mozilla多种浏览器
<html>
<head>
<title>Html转换程序</title>
<SCRIPTlanguage="JavaScript1.2">
functionHtmlToUBB(str){
str=str.replace(/\r/g,"");
str=str.replace(/on(load|click|dbclick|mouseover|mousedown|mouseup)="[^"]+"/ig,"");
str=str.replace(/<script[^>]*?>([\w\W]*?)<\/script>/ig,"");
str=str.replace(/<a[^>]+rel="nofollow noopener noreferrer" href="([^"]+)"[^>]*>(.*?)<\/a>/ig,"\n[url=$1]$2[/url]\n");
str=str.replace(/<font[^>]+color=([^>]+)[^>]*>(.*?)<\/font>/ig,"\n[color=$1]$2[/color]\n");
str=str.replace(/<img[^>]+src="([^"]+)"[^>]*>/ig,"\n[img]$1[/img]\n");
str=str.replace(/<([\/]?)b>/ig,"[$1b]");
str=str.replace(/<([\/]?)strong>/ig,"[$1b]");
str=str.replace(/<([\/]?)u>/ig,"[$1u]");
str=str.replace(/<([\/]?)i>/ig,"[$1i]");
str=str.replace(//g,"");
str=str.replace(/&/g,"&");
str=str.replace(/"/g,"\"");
str=str.replace(/</g,"<");
str=str.replace(/>/g,">");
str=str.replace(/<br>/ig,"\n");
str=str.replace(/<[^>]*?>/g,"");
str=str.replace(/\[url=([^\]]+)\]\n(\[img\]\1\[\/img\])\n\[\/url\]/g,"$2");
str=str.replace(/\n+/g,"\n");
returnstr;
} functiontrans(){
varstr="";
str=document.getElementById('edit').contentWindow.document.body.innerHTML;
if(str.length==0){
alert("无转换内容!请使用Ctrl+V把内容复制到下面的编辑框内");
}
document.getElementById('ReCode').value=HtmlToUBB(str);
} functionclearCode(){
document.getElementById('ReCode').value="";
} functionStart(){
document.getElementById('edit').contentWindow.document.designMode="on";
try{
document.getElementById('edit').contentWindow.document.execCommand("undo",false,null);
}catch(e){
alert("你的Mozilla浏览器不支持在线RichEditor");
}
} functionViewSource(Source){
varHtml;
if(Source){
document.getElementById('sCode').value=document.getElementById('edit').contentWindow.document.body.innerHTML;
document.getElementById('edit').style.display="none";
document.getElementById('sCode').style.display="";
document.getElementById('tip').innerText="修改粘贴后的网页源代码>>";
document.getElementById('tranButton').disabled="disabled"
}
else{
document.getElementById('edit').contentWindow.document.body.innerHTML=document.getElementById('sCode').value;
document.getElementById('edit').style.display="";
document.getElementById('sCode').style.display="none";
document.getElementById('edit').contentWindow.document.designMode="on";
document.getElementById('tip').innerText="请使用Ctrl+V把内容复制到这里>>";
document.getElementById('tranButton').disabled=""
}
}
</SCRIPT>
<style>
body{background:#688ABD;margin:2px;}
label{color:#fff;}
th{font-size:14px;font-family:verdana,宋体;font-weight:bold;color:fc6;text-align:left}
#sCode{width:400;height:150px;border:1pxsolid#1F2F47;margin-top:0px!important;margin-top:-1px;margin-bottom:0px!important;margin-bottom:-1px;}
#ReCode{width:400;height:150px;border:1pxsolid#1F2F47;font-size:12px;}
#edit{width:398;height:148px;border:1pxsolid#1F2F47;background:#fff;font-size:12px}
.input{
border-top:1pxsolid#fff;
border-right:1pxsolid#003;
border-bottom:1pxsolid#003;
border-left:1pxsolid#fff;
background:#F4F7FB;
}
.tip{
color:#ffc;
font-weight:bold;
font-size:12px;
font-family:宋体;
}
</style>
</head>
<bodyonload="Start()"style="">
<tableborder="0"align="center">
<tr>
<thstyle="">Html转换UBB程序</th>
</tr>
<tr>
<tdstyle="font-size:12px;">
<labelfor="Source"><inputtype="checkbox"id="Source"onclick="ViewSource(this.checked)">查看源代码</label>
<inputid="tranButton"type="button"value="转换"onclick="trans()"class="input">
<inputtype="button"value="清除"onclick="clearCode()"class="input"></td>
</tr>
<tr>
<tdclass="tip">
<spanid="tip">请使用Ctrl+V把内容复制到这里>></span></td>
</tr>
<tr>
<td>
<textareaid="sCode"style="display:none"></textarea>
<iframeid="edit"src="about:blank"border="0"frameborder="0"></iframe>
</td>
</tr>
<tr>
<tdclass="tip">
转换输出代码>></td>
</tr>
<tr>
<td>
<textareaid="ReCode"readonly="readonly"></textarea>
</td>
</tr>
</table>
</body>
</html>

本文简单的Html转换UBB的程序到此结束。我们停下脚步,其实只为走得更远!小编再次感谢大家对我们的支持!

您可能有感兴趣的文章
正则表达式匹配闭合HTML标签(支持嵌套)

正值表达式匹配html标签的属性值

积累比较常用的正则表达式(例如:匹配中文、匹配html)

收集一些常用的正则表达式(匹配中文字符、匹配双字节字符、匹配HTML标记、匹配空行 and so on~~~)

用正则查找html中有id属性的html标签