Turning on BBCODES - Javascript


Turning on BBCODES - Javascript

我正在使用这个聊天http://tutorialzine.com/2010/10/ajax-web-chat-php-mysql/,我试图打开bbcodes,但是:http://imageshack.us/photo/my-images/827/bbcodes1.png/

I tried replace in

scripts.js

 text       : text.replace(/</g,'&lt;').replace(/>/g,'&gt;')

text        : text.replace(/</g,'&lt;').
                                replace(/>/g,'&gt;').
                                replace(/'[b'](('s|.)+?)'['/b']/,'<b>''1</b>')

这是我的bbcode函数,你可以编辑codes变量来支持更多的bbcode。目前,它只支持[b],[我],[美国],[url], [img]。
[read]是我的网站的特殊bbcode

function bbcode(text){
    var codes = {};
    codes['url'] = {
        params : {
            href : ''
        },
        html: "<a href='"$href'">$text</a>"
    };
    codes['b'] = {
        params : {
        },
        html : "<b>$text</b>"
    };
    codes['i'] = {
        params : {
        },
        html : "<i>$text</i>"
    };
    codes['u'] = {
        params : {
        },
        html : "<u>$text</u>"
    };
    codes['img'] = {
        params : {
        },
        html : "<img src='"$text'" />"
    };
    codes['read'] = {
        params : {
        },
        text : function(text){
            for(var x in codes){
                text = text.replace(new RegExp('''[' + x + ''']','gi'), '').replace(new RegExp('''[''/' + x + ''']','gi'), '');
            }
            return encodeURIComponent(text);
        },
        process: function(text){
            return text;
        },
        html : "$text <object type='"application/x-shockwave-flash'" data='"" + GLOBALS.baseUrl + "/player_mp3_maxi.swf'" width='"200'" height='"20'"><param name='"movie'" value='"" + GLOBALS.baseUrl + "/player_mp3_maxi.swf'" /><param name='"bgcolor'" value='"#ffffff'" /><param name='"FlashVars'" value='"mp3=http://api.ispeech.org/api/rest?apikey%3Ddeveloperdemokeydeveloperdemokey%26action%3Dconvert%26voice%3Dusenglishfemale1%26text%3D$TEXT&amp;width=200&amp;showslider=1'" /></object>"
    };
    text = text.replace(/'</g, '&lt;').replace(/'>/g, '&gt;');
    var nomore = false;
    while(!nomore){
        var matches = text.match(/'[([^']]+)']/gi);
        if(matches == null)
            return text;
        nomore = true;
        for(var i = 0; i < matches.length; i++){
            var code = matches[i].substring(1, matches[i].length - 1);
            var parts = code.split(/'s+/);
            if(typeof codes[parts[0]] == 'object'){
                //is exist
                var obj = {};
                //get the params
                for(var j = 1; j < parts.length; j++)
                {
                    var temp = parts[j].split('=');
                    if(typeof codes[parts[0]].params[temp[0]] != 'undefined'){
                        obj[temp[0]] = (temp.length > 1) ? (temp[1].indexOf('"') == 0 ? temp[1].substring(1, temp[1].length - 1) : temp[1]) : '';
                    }
                }
                //find the text
                var index = text.indexOf(matches[i]);
                var index2 = text.indexOf('[/'+parts[0]+']', index);
                if(index2 == -1)
                    index2 = text.length;
                var t = text.substring(index + matches[i].length, index2);
                if(typeof codes[parts[0]].process == 'function'){
                    t = codes[parts[0]].process(t);
                }if(typeof codes[parts[0]].text == 'function'){
                    t2 = codes[parts[0]].text(t);
                }
                var html = codes[parts[0]].html;
                for(var x in obj)
                    html = html.replace("$" + x, obj[x]);
                html = html.replace(/'$text/g, t);
                if(typeof codes[parts[0]].text == 'function'){
                    html = html.replace(/'$TEXT/g, t2);
                }
                text = text.substr(0, index) + html + text.substr(index2 + parts[0].length + 3);
                nomore = false;
            }
        }
    }
    text = text.replace(/'n/g, "<br />");
    return text;
}

可以这样使用:

text: bbcode(text)