JS代码格式化和语法着色V2

2019-06-03 18:16:39王旭

                if(c.match(/W/)&&glbStr.charAt(i-1).match(/w/)){
                    str=str.substring(0,str.length-htmlEncode(c).length);
                    str=str.replace(regKW,clKW).replace(regObj,clObj).replace(regMP,clMP)+htmlEncode(c);
                }
                lastState="";
            break;
        }
    }

    if(i==glbStr.length){
        if(str!=""){ outputLn(str); str=" "; }
        stopExec();
    }

}

function outputLn(theStr){
    var seq, txt, fn;
    if(html2txt(theStr).match(/^[ trn]*$/))return;
    seq=document.createElement("p");
    seq.innerHTML=theStr;
    curRe.appendChild(seq);

    txt=html2txt(theStr);
    if(!txt)return;
    fn=txt.match(/function[ trn]+([.w]+?)[ trn]*(.*?)[ trn]*{/);
    if(fn){
        glbFuntionNames.push(fn[1]);
        selJSExport.options[selJSExport.options.length]=new Option(fn[0]+"}",fn[1]);
        selJSExport.options[selJSExport.options.length-1].obj=seq;
    }
    fn=txt.match(/([.w]+?)[ trn]*=[ trn]*functionW*(.*?)[ trn]*{/);
    if(fn){
        glbFuntionNames.push(fn[1]);
        selJSExport.options[selJSExport.options.length]=new Option(fn[0]+"}",fn[1]);
        selJSExport.options[selJSExport.options.length-1].obj=seq;
    }
}

function clKW(str){ return(str.charAt(0)+"<span class="keyWord">"+str.substring(1)+"</span>"); }

function clObj(str){ return(str.charAt(0)+"<span class="object">"+str.substring(1)+"</span>"); }

function clMP(str){ return(str.charAt(0)+"<span class="method_property">"+str.substring(1)+"</span>"); }