我不是Java语言专家,但是我开始认为这有点奇怪!本质上,我编写了以下函数:

 function onChange()
 {
      if(this.responseText.length != 0) {

       // Get the new HTML Page requested from the servlet.
       var currentHTML = new XMLSerializer().serializeToString(document);
       var newHTML = this.responseText;

       currentHTML = currentHTML.replace('/\s+/g','');
       currentHTML = currentHTML.replace('/[\n\r]/g','');
       currentHTML = currentHTML.replace('/\t/g','');

       newHTML = newHTML.replace('/\s+/g','');
       newHTML = newHTML.replace('/[\n\r]/g','');
       newHTML = newHTML.replace('/\t/g','');


       // (There's also some alerts here just to get the output)
 }


现在,当函数获取currentHTMLnewHTML的值时,它将通过正则表达式方法传递它们,该方法旨在去除所有空格,回车符和制表符。但是,这没有发生。没有错误,没有故障。通过并没有丝毫改变变量。

最佳答案

正则表达式文字不被引号引起来。您需要更改此:

currentHTML.replace('/\s+/g','');


对此:

currentHTML.replace(/\s+/g,'');


另外,您的替换品有点多余。 \s已经匹配制表符和换行符(以及空格!)。

09-07 15:35