Javascript代碼 — 去除一段string中的所有空行和空白行,內有javascript的trim實現

來源:互聯網
上載者:User
代碼如下,仔細看能看懂,用了一點RE的內容。js中,用//括起來的字串表示是一個RE字串。

    function updateStuffs() {
        var response;
        if (request.readyState == 4) {
            if (request.status == 200) {
                response = request.responseText;
                var content_array = response.split("==== DELIMITER ====");
                var peselect = document.getElementById("peselectid");
                var prjselect = document.getElementById("prjselectid");
                var masterqselect = document.getElementById("masterqselectid");
               
                var ls = /^\s*/;  // Leading space regular expression
                var ts = /\s*$/;  // Trailing space regular expression

                // Break the response into lines
                var lines = content_array[0].split("\n");
                var peselect_result = "";
                // Loop through the lines
                for(var i = 0; i < lines.length; i++) {
                    var line = lines[i];
                    if (line.length == 0) continue;  // Skip empty lines
                    line = line.replace(ls, "").replace(ts, "");
                    if (line.length == 0) continue;  // Skip space filling lines
                    peselect_result += line + "\n";
                }

                // Break the response into lines
                lines = content_array[1].split("\n");
                var prjselect_result = "";
                // Loop through the lines
                for(var i = 0; i < lines.length; i++) {
                    var line = lines[i];
                    if (line.length == 0) continue;  // Skip empty lines
                    line = line.replace(ls, "").replace(ts, "");
                    if (line.length == 0) continue;  // Skip space filling lines
                    prjselect_result += line + "\n";
                }
               
                // Break the response into lines
                lines = content_array[1].split("\n");
                var masterq_select_result = "";
                // Loop through the lines
                for(var i = 0; i < lines.length; i++) {
                    var line = lines[i];
                    if (line.length == 0) continue;  // Skip empty lines
                    line = line.replace(ls, "").replace(ts, "");
                    if (line.length == 0) continue;  // Skip space filling lines
                    masterq_select_result += line + "\n";
                }
               
                peselect.innerHTML = "<select name='pe_name' class='listselect'>" + peselect_result + "</select>";
                prjselect.innerHTML = "<select name='project' class='listselect'>" + prjselect_result + "</select>";
                masterqselect.innerHTML = "<select name='master_queue' class='listselect'>" + masterq_select_result + "</select>";
            } else if (request.status == 404) {
                alert("<bean:message key='error.ajax.urlnotexist'/>");
            } else {
                alert("<bean:message key='error.ajax.errorcode'/>" + request.status);
            }
        }
    }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.