ubb正则转换

// html 转 ubb
var htmlToBBCode = function (html) {
    html = html.replace(/\[/gi, "&lBrac");
    html = html.replace(/\]/gi, "&rBrac");
    // html = html.replace(//gi, "[b]");
    html = html.replace(//gi, "[i]");
    html = html.replace(//gi, "[u]");
    // html = html.replace(/<\/b>/gi, "[/b]");
    html = html.replace(/<\/i>/gi, "[/i]");
    html = html.replace(/<\/u>/gi, "[/u]");
    html = html.replace(//gi, "[s]");
    html = html.replace(/<\/s>/gi, "[/s]");
    html = html.replace(//gi, "[i]");
    html = html.replace(/<\/em>/gi, "[/i]");
    html = html.replace(//gi, "[b]");
    html = html.replace(/<\/strong>/gi, "[/b]");
    html = html.replace(/(.*?)<\/a>/gi, "[url=$2]$4[/url]");
    html = html.replace(/(.*?)<\/span>$/gi, "[$2]$4[/$2]");
    html = htmlToBBCodeParseSpan(html);

    return html;
}

var htmlToBBCodeParseSpan = function (html) {
    if (html.match(/]*>/gi)) {
        var found = html.match(/]*>[^>]*<\/span>/gi);
        found.forEach(function(v) {
            html = html.replace(v, htmlToBBCodeReplaceSpan(v));
        });
        return htmlToBBCodeParseSpan(html);
    } else {
        return html
    }
}

var htmlToBBCodeReplaceSpan = function (html) {
    html = html.replace(/(.*?)<\/span>/gi, "[font=$1]$2[/font]");
    html = html.replace(/(.*?)<\/span>/gi, "[size=$1]$2[/size]");
    html = html.replace(/(.*?)<\/span>/gi, "[color=$1]$2[/color]");
    html = html.replace(/(.*?)<\/span>/gi, "[bgcolor=$1]$2[/bgcolor]");
    html = html.replace(/(.*?)<\/span>/gi, "$1");

    return html;
}

// ubb 转 html
var BBCodeToHtml = function (bbcode) {
    bbcode = bbcode.replace(/&lBrac/gi, '[')
    bbcode = bbcode.replace(/&rBrac/gi, ']')
    bbcode = bbcode.replace(/&nbsp;/g, ' ')
    bbcode = bbcode.replace(/&lt;/g, '<')
    bbcode = bbcode.replace(/&gt;/g, '>')
    bbcode = bbcode.replace(/&amp;/g, '&')
    bbcode = bbcode.replace(/\[i\]/gi, "");
    bbcode = bbcode.replace(/\[\/i\]/gi, "");
    bbcode = bbcode.replace(/\[b\]/gi, "");
    bbcode = bbcode.replace(/\[\/b\]/gi, "");
    bbcode = bbcode.replace(/\[u\]/gi, "");
    bbcode = bbcode.replace(/\[\/u\]/gi, "");
    bbcode = bbcode.replace(/\[s\]/gi, "");
    bbcode = bbcode.replace(/\[\/s\]/gi, "");
    bbcode = bbcode.replace(/\[bgcolor=(.*?)\]/gi, '')
    bbcode = bbcode.replace(/\[font=(.*?)\]/gi, '')
    bbcode = bbcode.replace(/\[size=(.*?)\]/gi, '')
    bbcode = bbcode.replace(/\[color=(.*?)\]/gi, '')
    bbcode = bbcode.replace(/\[left\]/gi, '')
    bbcode = bbcode.replace(/\[center\]/gi, '')
    bbcode = bbcode.replace(/\[right\]/gi, '')
    bbcode = bbcode.replace(/\[\/(left|center|right)\]/gi, '')
    bbcode = bbcode.replace(/\[\/(font|size|bgcolor|color)\]/gi, "");

    bbcode = bbcode.replace(/\[url=(.*?)\](.*?)\[\/url\]/gi, '$2<\/a>')
    return bbcode;
}

// ubb对齐方式预览
function BBcodeToShare(bbcode) {

  bbcode = bbcode.replace(/\[left\]/gi, '

') bbcode = bbcode.replace(/\[center\]/gi, '

') bbcode = bbcode.replace(/\[right\]/gi, '

') bbcode = bbcode.replace(/\[\/(left|center|right)\]/gi, '

') bbcode = BBCodeToHtml(bbcode) return bbcode } // ubb清除 var BBCodeToEmpty = function (bbcode) { bbcode = bbcode.replace(/\[i\]/gi, ""); bbcode = bbcode.replace(/\[\/i\]/gi, ""); bbcode = bbcode.replace(/\[b\]/gi, ""); bbcode = bbcode.replace(/\[\/b\]/gi, ""); bbcode = bbcode.replace(/\[u\]/gi, ""); bbcode = bbcode.replace(/\[\/u\]/gi, ""); bbcode = bbcode.replace(/\[s\]/gi, ""); bbcode = bbcode.replace(/\[\/s\]/gi, ""); bbcode = bbcode.replace(/\[bgcolor=(.*?)\]/gi, '') bbcode = bbcode.replace(/\[font=(.*?)\]/gi, '') bbcode = bbcode.replace(/\[size=(.*?)\]/gi, '') bbcode = bbcode.replace(/\[color=(.*?)\]/gi, '') bbcode = bbcode.replace(/\[left\]/gi, '') bbcode = bbcode.replace(/\[center\]/gi, '') bbcode = bbcode.replace(/\[right\]/gi, '') bbcode = bbcode.replace(/\[\/(left|center|right)\]/gi, '') bbcode = bbcode.replace(/\[\/(font|size|bgcolor|color)\]/gi, ""); bbcode = bbcode.replace(/\[url=(.*?)\](.*?)\[\/url\]/gi, '') return bbcode; }

你可能感兴趣的:(ubb正则转换)