Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
奇怪的Javascript字符串操作行为_Javascript_String - Fatal编程技术网

奇怪的Javascript字符串操作行为

奇怪的Javascript字符串操作行为,javascript,string,Javascript,String,我不是Javascript专家,但我开始觉得这有点奇怪!基本上,我编写了这个函数: function onChange() { if(this.responseText.length != 0) { // Get the new HTML Page requested from the servlet. var currentHTML = new XMLSerializer().serializeToString(document);

我不是Javascript专家,但我开始觉得这有点奇怪!基本上,我编写了这个函数:

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

现在,当函数获取
currentHTML
newHTML
的值时,它将通过regex方法传递这些值,这些方法用于去除所有空格、回车和制表符。然而,这并没有发生。没有错误,就没有错误。通过,它不会丝毫改变变量。

正则表达式文本不会被引号包围。您需要更改以下内容:

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

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

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

正则表达式文本没有被引号包围。您需要更改以下内容:

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

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

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

我想您忘记了关闭if body

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


}

我想你忘了关上你的身体

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


}

砰的一声。我就知道这会很烦人。威尔·马克7分钟后:)砰的一声。我就知道这会很烦人。威尔·马克7分钟后:)我想我在提取相关代码时删除了它。谢谢!+1我想我在提取相关代码时删除了它。谢谢!+1