Html转UBB的JS版本【寻求更好的方案】

Html转UBB的JS版本【寻求更好的方案】

简介:Html转UBB的JS版本【寻求更好的方案】

             转换部分,存在BUG。。。

------------------------------------------------------------------------------------------------------------------------------------------------------

Html代码:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>HtmlUbbTranslation</title>

    <script src="js/Html_Ubb.js" type="text/javascript"></script>

</head>
<body>
    <form id="form1" runat="server">
    <div style="padding: 5px; text-align: center;">
        <div>
            <div>
                请将 <strong>Html</strong> 源代码拷贝到下面表单中:
            </div>
            <div>
                <textarea id="Hsource" cols="15" rows="15" style="width: 700px;" οnkeyup="htmltoubb()"
                    οnfοcus="htmltoubb()"></textarea>
            </div>
            <span>下面表单中是相应的 <strong>UBB</strong> 代码: </span>
            <br>
            <span>
                <textarea id="Uresult" cols="15" rows="15" style="width: 700px;"></textarea>
            </span>
        </div>
    </div>
    </form>
</body>
</html>

------------------------------------------------------------------------------------------------------------------------------------------------------

JavaScript脚本:

	function htmltoubb(){
		str = pattern(document.getElementById("Hsource").value);
		document.getElementById("Uresult").value=str;
	}
	
function pattern(str){
	//str = str.replace(/(\r\n|\n|\r)/ig, '');
	str = str.replace(/<br[^>]*>/ig,'\n');
	str = str.replace(/<p[^>\/]*\/>/ig,'\n');
	//str = str.replace(/\[code\](.+?)\[\/code\]/ig, function($1, $2) {return phpcode($2);});	
	str = str.replace(/\son[\w]{3,16}\s?=\s*([\'\"]).+?\1/ig,'');

	str = str.replace(/<hr[^>]*>/ig,'[hr]');
	str = str.replace(/<(sub|sup|u|strike|b|i|pre)>/ig,'[$1]');
	str = str.replace(/<\/(sub|sup|u|strike|b|i|pre)>/ig,'[/$1]');
	str = str.replace(/<(\/)?strong>/ig,'[$1b]');
	str = str.replace(/<(\/)?em>/ig,'[$1i]');
	str = str.replace(/<(\/)?blockquote([^>]*)>/ig,'[$1blockquote]');

	str = str.replace(/<img[^>]*smile=\"(\d+)\"[^>]*>/ig,'[s:$1]');
	str = str.replace(/<img[^>]*src=[\'\"\s]*([^\s\'\"]+)[^>]*>/ig,'[img]'+'$1'+'[/img]');
	str = str.replace(/<a[^>]*href=[\'\"\s]*([^\s\'\"]*)[^>]*>(.+?)<\/a>/ig,'[url=$1]'+'$2'+'[/url]');
	//str = str.replace(/<h([1-6]+)([^>]*)>(.*?)<\/h\1>/ig,function($1,$2,$3,$4){return h($3,$4,$2);});

	str = str.replace(/<[^>]*?>/ig, '');
	str = str.replace(/&/ig, '&');
	str = str.replace(/</ig, '<');
	str = str.replace(/>/ig, '>');

	return str;
}

function up(str){

		str = str.replace(/</ig,'<');
		str = str.replace(/>/ig,'>');
		str = str.replace(/\n/ig,'<br />');
		str = str.replace(/\[code\](.+?)\[\/code\]/ig, function($1, $2) {return phpcode($2);});

		str = str.replace(/\[hr\]/ig,'<hr />');
		str = str.replace(/\[\/(size|color|font|backcolor)\]/ig,'</font>');
		str = str.replace(/\[(sub|sup|u|i|strike|b|blockquote|li)\]/ig,'<$1>');
		str = str.replace(/\[\/(sub|sup|u|i|strike|b|blockquote|li)\]/ig,'</$1>');
		str = str.replace(/\[\/align\]/ig,'</p>');
		str = str.replace(/\[(\/)?h([1-6])\]/ig,'<$1h$2>');

		str = str.replace(/\[align=(left|center|right|justify)\]/ig,'<p align="$1">');
		str = str.replace(/\[size=(\d+?)\]/ig,'<font size="$1">');
		str = str.replace(/\[color=([^\[\<]+?)\]/ig, '<font color="$1">');
		str = str.replace(/\[backcolor=([^\[\<]+?)\]/ig, '<font style="background-color:$1">');
		str = str.replace(/\[font=([^\[\<]+?)\]/ig, '<font face="$1">');
		str = str.replace(/\[list=(a|A|1)\](.+?)\[\/list\]/ig,'<ol type="$1">$2</ol>');
		str = str.replace(/\[(\/)?list\]/ig,'<$1ul>');

		str = str.replace(/\[s:(\d+)\]/ig,function($1,$2){ return smilepath($2);});
		str = str.replace(/\[img\]([^\[]*)\[\/img\]/ig,'<img src="$1" border="0" />');
		str = str.replace(/\[url=([^\]]+)\]([^\[]+)\[\/url\]/ig, '<a href="$1">'+'$2'+'</a>');
		str = str.replace(/\[url\]([^\[]+)\[\/url\]/ig, '<a href="$1">'+'$1'+'</a>');
		return str;
	}

	function ubbtohtml(){
		str = up(document.getElementById("Usource").value);
		document.getElementById("Hresult").value=str;
	}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值