Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/426.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
为什么string.replace(//gm,函数{…}无法删除javascript中带有'backticks'的多行字符串中的换行符_Javascript_Regex_Backticks_Multilinestring - Fatal编程技术网

为什么string.replace(//gm,函数{…}无法删除javascript中带有'backticks'的多行字符串中的换行符

为什么string.replace(//gm,函数{…}无法删除javascript中带有'backticks'的多行字符串中的换行符,javascript,regex,backticks,multilinestring,Javascript,Regex,Backticks,Multilinestring,请运行完整的功能代码片段 function nlin(x){ // I have to use: return x.replace(/[\s\S]/gm, function(e) { return x.replace(/./gm, function(e) { /* the new var e is never 10==e.charCodeAt(0) ! ** THAT is PROBLEM and ANSWER **

请运行完整的功能代码片段

function nlin(x){ 


// I have to use:  return x.replace(/[\s\S]/gm, function(e) {
return x.replace(/./gm, function(e) {
    /*
    
      the new var e is never 10==e.charCodeAt(0) !
      
      ** THAT is PROBLEM and ANSWER **
      
      @ VLAZ EDIT: I was too tired and didn't recognize @ VLAZ's answer:
      "The . character doesn't match any newline characters"
      regex /./ change to  "use [\s\S] (any whitespace or non-whitespace)
      to match everything"  
      would be elegant
      // I have to use:  return x.replace(/[\s\S]/gm, function(e) {       
      but I wrote additional code..
      
    
    */
    // the new var e contains one char after one , like while  , 
    // but I do not have to find out the count/size of var x
    // I could also use "x" instead of "e", it works exactly the same way ,
    // the value of x is available in the new variable e, 
    // in the function brackets function(e), 
    // so far I have not found any explanation for this javascript behavior.
    // You can often find it in jQuery:
    // line:
    // 1:   jQuery v1.8.3 jquery.com | jquery.org/license   
    // 25:  }if(t.nodeType){return v.grep(e,function(e,r){return e===t===n;
    // 27:  }if(typeof t=="string"){var r=v.grep(e,function(e){return e.nodeType===1;
    // 
    console.log('in f(e): e.0  '+e.charCodeAt(0) +'  '+e[0] );
    console.log('in f(e): e.1  '+e.charCodeAt(1) +'  '+e[1]  );
    if( inside_e ){ e=e.replace(/\n/, ''); console.log('   [in]');} 

    return e;   
});};
为什么string.replace(//gm,函数(e){…}不能删除多行字符串中带有
反勾号的换行符
在“return x.replace(//gm,函数(e)”内部,但在外部可以

。。。 我编辑并得到了答案

Why string.replace(//gm,函数){…}不能删除带有“backticks”的多行字符串中的换行符,但可以删除其他字符
与新线----
替换外部函数(e){}
函数(e){replace}
换行符删除了吗------
变量x,外部=0,内部=0;
函数nlin(x){
如果(外部){x=x.replace(/\n/,'');console.log('[out]');}
log('f(e)之外:x.0'+x.charCodeAt(0)+'+x[0]+'(10是换行符)');
console.log('f(e)外部:x.1'+x.charCodeAt(1)+''+x[1]);
//我必须使用:return x.replace(/[\s\s]/gm,函数(e){
返回x.更换(//总经理,功能(e){
/*
新的变量e永远不会是10==e.charCodeAt(0)!
**这就是问题和答案**
@VLAZ编辑:我太累了,没有认出@VLAZ的答案:
“.字符与任何换行符都不匹配”
正则表达式//更改为“使用[\s\s](任何空格或非空格)
匹配一切”
会很优雅
//我必须使用:return x.replace(/[\s\s]/gm,函数(e){
但是我写了额外的代码。。
*/
//新的变量e包含一个接一个的字符,比如while,
//但我不必找出var x的计数/大小
//我也可以用“x”代替“e”,它的工作原理完全相同,
//x的值在新变量e中可用,
//在函数括号中,函数(e),
//到目前为止,我还没有找到对这种javascript行为的任何解释。
//您通常可以在jQuery中找到它:
//行:
//1:jQuery v1.8.3 jQuery.com | jQuery.org/license
//25:}if(t.nodeType){返回v.grep(e,函数(e,r){返回e==t==n;
//27:}if(typeof t==“string”){var r=v.grep(e,函数(e){returne e.nodeType==1;
// 
log('in f(e):e.0'+e.charCodeAt(0)+''+e[0]);
console.log('in f(e):e.1'+e.charCodeAt(1)+''+e[1]);
if(in_e){e=e.replace(/\n/,'');console.log('[in]');}
返回e;
});
};
_func()中的函数{inside_e=1;outside_e=0;run();}
函数out_func(){outside_e=1;inside_e=0;run();}
函数运行(){
//x多行字符串中带换行符的反勾号
x=`
y`;
document.getElementById(“out”).innerHTML=x;
x=nlin(x);
document.getElementById(“out2”).innerHTML=x;
console.log(“”);console.log(“”);
}
运行(0);

我编辑了完整的代码片段,并用两个按钮显示了有效的代码行-与无效的旧位置相比

@弗拉兹 编辑: 我太累了,没有认出@VLAZ的答案

regex//更改为 “使用[\s\s](任何空白或非空白)匹配所有内容”
将是优雅的;我写了额外的代码< /p> ->“内联代码”。这个问题有点混乱。请考虑查看未定义/未声明变量的代码示例(在第一个示例中,什么是代码> S/<代码>?)<代码> >(0)…<代码>是NO-OP。这是您想要的吗?(在第一个示例中,什么是S?)--这只是为了澄清函数的外观。。请参阅运行代码片段-完整功能。
字符默认情况下不匹配任何换行符。一直以来,只有较新的标准添加了
s
标志,允许
匹配换行符。您也可以使用
[\s\s]
(任何空白或非空白)匹配所有内容。虽然此代码可能会解决问题,但如何以及为什么解决此问题将真正有助于提高您的帖子质量,并可能导致更多的投票。请记住,您是在将来为读者回答问题,而不仅仅是现在问问题的人。请您的答案添加解释并给出答案说明适用的限制和假设。