ubb标签转html(dz论坛也支持)

在开发过程中,有很多论坛把数据格式转成了ubb格式

[img]图片地址[/img]等等~~

在网上找到了一个例子

    <script>
       //str代表字符串   html转ubb    pattern(字符串)即可
        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(/&amp;/ig, '&');
            str = str.replace(/&lt;/ig, '<');
            str = str.replace(/&gt;/ig, '>');

            return str;
        }
         //str代表字符串   ubb转html    up(字符串)即可
        function up(str){

            str = str.replace(/</ig,'&lt;');
            str = str.replace(/>/ig,'&gt;');
            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;
        }

        
    </script>

上面的up方法已经兼容很大一部分了但是有个别的论坛可能繁琐点,我做出了点修改和添加。
注:因为适配原因str = str.replace(/[size=(\d+?)]/ig,'<font size="$1">'); 我把 “ $1”改成了2.具体根据自己的需求改动

  //str代表字符串   ubb转html    up(字符串)即可
  function up(str){

    str = str.replace(/</ig,'&lt;');
    str = str.replace(/>/ig,'&gt;');
    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="2">');
    str = str.replace(/\[size=(\d+?.\d+?)pt\]/ig,'<font size="2">');
    str = str.replace(/\[size=(\d+?)pt\]/ig,'<font size="2">');
    str = str.replace(/\[size=(\d+?)px\]/ig,'<font size="2">');
    str = str.replace(/\[p=(\d+?), null, center\]/ig,'<font size="1" style="text-align:center;">');
    str = str.replace(/\[i=s\]/ig,'');
    str = str.replace(/\[color=([^\[\<]+?)\]/ig, '<font style="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('&lt;','<').replace('&gt;','>');

    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;
}

前端小白,有意见请@我~~~

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 工厂模式类似于现实生活中的工厂可以产生大量相似的商品,去做同样的事情,实现同样的效果;这时候需要使用工厂模式。简单...
    舟渔行舟阅读 12,385评论 2 17
  • 以下是常用的代码收集,学习用。转自豪情博客园 1. PC - js 返回指定范围的随机数(m-n之间)的公式 re...
    自由加咖啡阅读 4,638评论 0 1
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,811评论 25 709
  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 14,687评论 1 92
  • <a name='html'>HTML</a> Doctype作用?标准模式与兼容模式各有什么区别? (1)、<...
    clark124阅读 8,952评论 1 19