HTML教程:将html 转成ubb代码的小脚本
21视频教程网收集整理
进入HTML视频教程专区
呵呵,这几天沉溺于灌水,发现转贴的时候真的是很不方便,文字、图形、颜色、连接,如果都转过来真的是满费劲的,于是就写了一个小东西,简陋的很,不过倒是可以省一些事
只写了几个常用的标签,如果要添加其他标签,按照已有的正则照猫画虎 (存储于剪切板的超文本和源文件有可能是不同的!!也就是说浏览器已经对原有 html 进行了规范化)
运行代码框 <script Language="JavaScript1.2"> //coded by windy_sk <windy_sk@126.com> 20031218 function html_trans(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[^>]+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"); return str; } function trans(){ var str = ""; rtf.focus(); rtf.document.body.innerHTML = ""; rtf.document.execCommand("paste"); str = rtf.document.body.innerHTML; if(str.length == 0) { alert("剪切版不存在超文本数据!"); return ""; } return html_trans(str); } </script> <textarea style="width:100%; height:200px" id="text" class="textarea" onbeforepaste="if(document.getElementById('x_paste').checked){window.clipboardData.setData('text',trans());this.focus();}"> 启用超文本粘贴转换