пожалуйста, запустите полный фрагмент функционального кода .
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, function (e) {...} не может удалить новую строку в многострочная строка с backticks
внутри внутри «return x.replace (/./ gm, function (e)», но снаружи может.
... Я отредактировал и получил ответ ...
<!DOCTYPE html>Why string.replace(/./gm,function(s){...} can not remove newline in multiline string with `backticks` , but can remove other char
with_newline----
newline_removed?------
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 );