\n 换行符不属于 . text.replace(/(\".*?\")/g 即你的 .* 是不包含换行符的, 改一种写法:
text.replace(/(\"[^\"]*?\")/g

解决方案 »

  1.   

    var bb_code_li_1 = '<li>';
    var bb_code_li_2 = '</li>';
    var text = document.all.CODE.innerHTML;
    text = text.replace(/<br>/gi, "\n");
    text = text.replace(/(\"(.|\n)*?\")/g, "<font color=red>$1</font>");
    text = text.replace(/(\'.*?\')/g, "<font color=red>$1</font>");
    text = text.replace(/([^color]=|\(|\)|,)/gi, "<font color=green>$1</font>");
    text = text.replace(/(\/\*(\n|\t|.)*\*\/)/gi, "<font color=#ff9900>$1</font>");
    text = text.replace(/\n/g, "\n" + bb_code_li_2 + bb_code_li_1);
    text = '<ol><li>' + text + '</ol>';
    document.all.CODE.innerHTML = "<font color=#0000ff>"+text+"</font>";
      

  2.   

    <style type="text/css">
    #CODE { padding-right: 0.5em; margin-top: 1em; font-size: 9pt; margin-bottom: 1em; margin-left: 0pt; padding-top: 0.5em; font-family: Courier New; background-color: #FFFFFF; color: #465584; border:1px solid #000000; border-left:6px solid #666666; }
    </style>
    <table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr>
      <td id='CODE'>&lt;?php<br>
            /*hello<br>
             */<br>
           &#036;string = implode&#40;&#34;&#092;n&#34;, &nbsp;&#036;array&#41;;//拆分<br>
           &#036;fp = fopen&#40;&#34;test.txt&#34;, &#34;w&#34;&#41;;<br>
           #写入<br>
            fwrite&#40;&#036;fp, &#036;string&#41;;<br>
            $abc = &quot;keysdf = sdf (sdf)<br>
            sdkfjsdf   klj l sdfj <br>
            abc
            &quot;; <br>
            ?&gt;
        </td>
    </tr></table><script language="javascript">
    var bb_code_li_1 = '<li>';
    var bb_code_li_2 = '</li>';
    var text = document.all.CODE.innerHTML;
    text = text.replace(/<br>/gi, "\n");
    text = text.replace(/(\"(.|\n)*?\")/g, "<font color=red>$1</font>");
    text = text.replace(/(\'.*?\')/g, "<font color=red>$1</font>");
    text = text.replace(/([^color]=|\(|\)|,)/gi, "<font color=green>$1</font>");
    text = text.replace(/(\/\*(\n|\t|.)*\*\/|\/\/.*\n|#.*\n)/gi, "<font color=#ff9900>$1</font>");
    text = text.replace(/\n/g, "\n" + bb_code_li_2 + bb_code_li_1);
    text = '<ol><li>' + text + '</ol>';
    document.all.CODE.innerHTML = "<font color=#0000ff>"+text+"</font>";
    //alert(text);
    </script>
    不好意思,再看看。
    对正则不是很熟悉。
    除了上面说的问题之外,
    还有在引号或者单引号中的=号和其它号不应该被替换,如何搞定好呢?
    多谢了。
      

  3.   

    php有现在的函数为什么不用?
    语法高亮还是挺麻烦的,简单的正则替换我认为实现不了。highlight_string();
    highlight_file();
    看看这两个函数
      

  4.   

    这两个函数我知道的。
    只是想试着用JS来试试看。
    这样子离开PHP也能为PHP代码着色了。