Javascript代码 -- 去除一段string中的所有空行和空白行,内有javascript的trim实现

代码如下,仔细看能看懂,用了一点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);
            }
        }
    }

你可能感兴趣的:(JavaScript)