为什么 string.replace(/./gm,function(s){...} 无法在 javascript 中使用“反引号”删除多行字符串中的换行符
Why string.replace(/./gm,function(s){...} can not remove newline in multiline string with `backticks` in javascript
请运行完整的功能代码片段。
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
的多行字符串中的换行符 inside inside " return x.replace(/./gm, function(e) " 但外面可以。
...
我编辑并得到了答案...
<!DOCTYPE html><html><body><div>Why string.replace(/./gm,function(s){...} can not remove newline in multiline string with `backticks` , but can remove other char</div><pre>
<div>with_newline--<span id="out"></span>--</div><div>
<button onclick="out_func()">replace outside function(e){}</button></div><div>
<button onclick="in_func()"> in function(e){replace} </button></div>
<div>newline_removed?----<span id="out2"></span>--</div>
<script type="text/javascript">
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 );
</script></pre></body></html>
我编辑了完整的 代码片段 并用两个 Buttn 显示了有效的代码行 - 与无效的旧位置。
@弗拉兹
编辑:
太累了,没认出@VLAZ的回答
正则表达式/./改为
“使用 [\s\S](任何空格或非空格)来匹配所有内容”
会很优雅;我写了额外的代码
请运行完整的功能代码片段。
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
的多行字符串中的换行符 inside inside " return x.replace(/./gm, function(e) " 但外面可以。
... 我编辑并得到了答案...
<!DOCTYPE html><html><body><div>Why string.replace(/./gm,function(s){...} can not remove newline in multiline string with `backticks` , but can remove other char</div><pre>
<div>with_newline--<span id="out"></span>--</div><div>
<button onclick="out_func()">replace outside function(e){}</button></div><div>
<button onclick="in_func()"> in function(e){replace} </button></div>
<div>newline_removed?----<span id="out2"></span>--</div>
<script type="text/javascript">
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 );
</script></pre></body></html>
我编辑了完整的 代码片段 并用两个 Buttn 显示了有效的代码行 - 与无效的旧位置。
@弗拉兹 编辑: 太累了,没认出@VLAZ的回答
正则表达式/./改为 “使用 [\s\S](任何空格或非空格)来匹配所有内容” 会很优雅;我写了额外的代码