1

I'm not a Javascript expert, but I'm starting to think this is somewhat strange! Essentially, I'd written this function:

 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)
 }

Now, when the function obtains values for currentHTML and newHTML, it's passing them through the regex methods, that are designed to strip out all the spaces, carriage returns and tabs. However, this is not happening. No errors, no faults. Passing through and it's not changing the variables in the slightest.

4

2 回答 2

5

正则表达式文字不被引号包围。你需要改变这个:

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

对此:

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

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

于 2013-07-04T09:17:44.707 回答
1

我想你已经忘记关闭如果身体。

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)

}

于 2013-07-04T09:20:15.713 回答