【发布时间】:2020-11-02 11:46:10
【问题描述】:
请运行完整的功能代码 sn-p。
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><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>
【问题讨论】:
-
这个问题有点乱。请考虑查看未定义/未声明变量的代码示例(在第一个示例中,
s是什么?)。if(0) ...是无操作的。这是你想要的吗? -
(在第一个例子中,什么是 s?)——这只是为了澄清,函数的样子。 .请查看运行代码 sn-p - 全功能。
-
.字符默认不匹配任何换行符。情况一直如此,只有较新的标准添加了s标志,它允许.匹配换行符。您还可以使用[\s\S](任何空格或非空格)来匹配所有内容。
标签: javascript regex backticks multilinestring