title: Why string.replace(/./gm,function(s){...} can not remove newline in multiline string with `backticks` in javascript
body: <p><strong>plese run the full functional  code snippet</strong>.</p>
  <pre><code>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:
        &quot;The . character doesn't match any newline characters&quot;
        regex /./ change to  &quot;use [\s\S] (any whitespace or non-whitespace)
        to match everything&quot;  
        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 &quot;x&quot; instead of &quot;e&quot;, 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==&quot;string&quot;){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;   
  });};
  </code></pre>
  <p>Why string.replace(/./gm,function(e){...} can not remove newline in multiline string with <code>backticks</code> inside  inside &quot; return x.replace(/./gm, function(e)  &quot; but outside can.</p>
  <p>...
  I edited and have the answer...</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;!DOCTYPE html&gt;&lt;html&gt;&lt;body&gt;&lt;div&gt;Why string.replace(/./gm,function(s){...} can not remove newline in multiline string with `backticks` , but can remove other char&lt;/div&gt;&lt;pre&gt;
  &lt;div&gt;with_newline--&lt;span id="out"&gt;&lt;/span&gt;--&lt;/div&gt;&lt;div&gt;
  &lt;button onclick="out_func()"&gt;replace outside function(e){}&lt;/button&gt;&lt;/div&gt;&lt;div&gt;
  &lt;button onclick="in_func()"&gt; in function(e){replace} &lt;/button&gt;&lt;/div&gt;
  &lt;div&gt;newline_removed?----&lt;span id="out2"&gt;&lt;/span&gt;--&lt;/div&gt;
  &lt;script type="text/javascript"&gt;
  
  var x, outside_e=0, inside_e=0; 
  function nlin(x){ 
      if( outside_e ){ x=x.replace(/\n/, ''); console.log('   [out]');}       
      console.log('outside f(e): x.0  '+x.charCodeAt(0) +'  '+x[0]+'( 10 is NewLine)' );
      console.log('outside f(e): x.1  '+x.charCodeAt(1) +'  '+x[1]  );
        
      // 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;   
      });
  
  };
  function in_func(){ inside_e=1; outside_e=0; run(); }
  function out_func(){ outside_e=1;inside_e=0; run(); }
  function run(){
      // x multi_line_string_in_backticks_with_newline
      x=`
  y`;     
              
      document.getElementById("out").innerHTML = x;
  
      x=nlin(x)   ;
      
      document.getElementById("out2").innerHTML = x;
      console.log(' ');console.log(' ');                  
  }
  run( 0 );
  &lt;/script&gt;&lt;/pre&gt;&lt;/body&gt;&lt;/html&gt;</code></pre>
  </div>
  </div>
  </p>