Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/393.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
用JavaScript字符串中的空值替换空值_Javascript_Regex - Fatal编程技术网

用JavaScript字符串中的空值替换空值

用JavaScript字符串中的空值替换空值,javascript,regex,Javascript,Regex,给定以下字符串: var str=[,,,1,2,,,3,4,,,,,,5,6] 我想用空值替换所有空值。换句话说,我想要这个结果: [null,null,null,1,2,null,null,3,4,null,null,null,null,null,null,5,6] 这几乎可以工作,但它忽略了第一个空值: var str = "[,,,1,2,,,3,4,,,,,,5,6]"; str.split(',').map(function(x) { return x ? x : 'null' })

给定以下字符串:

var str=[,,,1,2,,,3,4,,,,,,5,6]

我想用空值替换所有空值。换句话说,我想要这个结果:

[null,null,null,1,2,null,null,3,4,null,null,null,null,null,null,5,6]

这几乎可以工作,但它忽略了第一个空值:

var str = "[,,,1,2,,,3,4,,,,,,5,6]";
str.split(',').map(function(x) { return x ? x : 'null' }).join(',')
// Gives [,null,null,1,2,null,null,3,4,null,null,null,null,null,5,6]
同样,我注意到如果我有尾随的空值,它也会遗漏最后一个值:

var str = "[,,,1,2,,,3,4,,,,,,5,6,,]";
str.split(',').map(function(x) { return x ? x : 'null' }).join(',')
// Gives [,null,null,1,2,null,null,3,4,null,null,null,null,null,5,6,null,]
有没有办法确保第一个和最后一个空值也被替换

谢谢

这将起作用:

var str=[,,,1,2,,,3,4,,,,,,5,6]; var result='['+str.replace/^\[\]$/g,.split/[,]/.mapfunctionx{return x?x:'null'}.join','+']'; console.logresult 这将起作用:

var str=[,,,1,2,,,3,4,,,,,,5,6]; var result='['+str.replace/^\[\]$/g,.split/[,]/.mapfunctionx{return x?x:'null'}.join','+']';
console.logresult 您已删除所有空值。[和]不是空的。

您已删除所有空值。[和]并不是什么都没有。

秘密在于,在拆分字符串时,括号与逗号/数字在字符串的开头和结尾结合在一起。为了解决这个问题,您需要在拆分之前删除括号

这可以在/[\][]/g上完成:

var str=[,,1,2,,,3,4,,,,,,5,6,]; console.log str.replace/[\][]/g、.split'、'.mapfunctionx{ 返回x?x:'null' }.加入','
; 秘密在于,在拆分字符串时,括号会与字符串开头和结尾的逗号/数字组合在一起。为了解决这个问题,您需要在拆分之前删除括号

这可以在/[\][]/g上完成:

var str=[,,1,2,,,3,4,,,,,,5,6,]; console.log str.replace/[\][]/g、.split'、'.mapfunctionx{ 返回x?x:'null' }.加入',' ; var str=[,,,1,2,,,3,4,,,,,,5,6]; var a=`[${str.slice1,-1.split,.mapn=>n | | null}]` console.loga var str=[,,,1,2,,,3,4,,,,,,5,6]; var a=`[${str.slice1,-1.split,.mapn=>n | | null}]`
console.loga 如前所述,[和]正在更改返回值-这将删除它们并使用与现有函数相同的函数-但在缩短的字符串上,并返回正确的值

var str=[,,,1,2,,,3,4,,,,,,5,6]; 让newStr=str.substring1,str.length-1.split','.mapfunctionx{return x?x:'null'}.join',' console.lognewStr; //给出[null,null,null,1,2,null,null,3,4,null,null,null,null,null,5,6] var str=[,,1,2,,,3,4,,,,,,,5,6,,]; newStr=str.substring1,str.length-1.split','.mapfunctionx{return x?x:'null'}.join','; console.lognewStr;
//给出null,null,null,1,2,null,null,null,3,4,null,null,null,null,5,6,null,null,null如前所述,[和]正在更改返回值-这将删除它们并使用与现有函数相同的函数-但在缩短的字符串上,并返回正确的值

var str=[,,,1,2,,,3,4,,,,,,5,6]; 让newStr=str.substring1,str.length-1.split','.mapfunctionx{return x?x:'null'}.join',' console.lognewStr; //给出[null,null,null,1,2,null,null,3,4,null,null,null,null,null,5,6] var str=[,,1,2,,,3,4,,,,,,,5,6,,]; newStr=str.substring1,str.length-1.split','.mapfunctionx{return x?x:'null'}.join','; console.lognewStr; //给出null,null,null,1,2,null,null,3,4,null,null,null,null,null,5,6,null,null,null String.prototype.repEmptyWithNull= 函数{返回此 .replace/,/g',null,' .replace/,/g',null,' .替换“[,”,“[null,” .replace',]',null]' } console.log[,,1,2,,,3,4,,,,,,,5,6,].repEmptyWithNull console.log[,,1,2,,,3,4,,,,,,5,6]。repEmptyWithNull console.log[,1,2,,,3,4,,,,,,,5,6,,],repEmptyWithNull console.log[,1,2,,,3,4,,,,,,5,6]。repEmptyWithNull console.log[1,2,3,4,5,6,6],repEmptyWithNull console.log[1,2,3,4,5,6]。repEmptyWithNull String.prototype.repEmptyWithNull= 函数{返回此 .replace/,/g',null,' .replace/,/g',null,' .替换“[,”,“[null,” .replace',]',null]' } console.log[,,1,2,,,3,4,,,,,,,5,6,].repEmptyWithNull console.log[,,1,2,,,3,4,,,,,,5,6]。repEmptyWithNull console.log[,1,2,,,3,4,,,,,,,5,6,,],repEmptyWithNull console.log[,1,2,,,3,4,,,,,,5,6]。repEmptyWithNull console.log[1,2,3,4,5,6,6],repEmptyWithNull
console.log[1,2,3,4,5,6].repEmptyWithNullRemove the[]和Tester一切正常如果您只使用str.split',',则确实删除了所有空值。mapfunctionx{return x?x:'null'}删除[]和Tester一切正常如果您只使用str.split',则确实删除了所有空值,“.mapfunctionx{return x?x:'null'}啊,这很有意义。啊!非常感谢。啊,这是有道理的。啊!非常感谢。