Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/396.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/71.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 如何使用空格将变量拆分为4个字符?_Javascript_Html - Fatal编程技术网

Javascript 如何使用空格将变量拆分为4个字符?

Javascript 如何使用空格将变量拆分为4个字符?,javascript,html,Javascript,Html,我有一个变量,它保存了一个特定的数字,我希望它在显示中将其拆分为4个字符 document.getElementById("txtFNF").innerHTML = "<br>" + value.match(/.{1,4}/g) + "<br><br>"; for (let index = 1; index <= value.length; index++) { if (Number(value[value.length - index

我有一个变量,它保存了一个特定的数字,我希望它在显示中将其拆分为4个字符

document.getElementById("txtFNF").innerHTML = "<br>" + value.match(/.{1,4}/g) + "<br><br>";
   for (let index = 1; index <= value.length; index++) {
      if (Number(value[value.length - index]) == 1) {
         document.getElementById("txtFNF").innerHTML += index - 1 + " - " + FNFtoString(index - 1) + "<br>";
 }
document.getElementById(“txtfnfnf”).innerHTML=“
”+value.match(/.{1,4}/g)+“

”; 对于(让index=1;index当您使用
“”+[]
时,它会在数组内部调用
toString
,从而将其更改为由
连接的字符串,


console.log(“+[1,2,3,4])
您可以使用join方法将逗号分隔转换为如下空格

<div id="txtFNF" class="btn--success"></div>
<script>
    let value = '12345678';
    let val = value.match(/.{1,4}/g).join(' ', ',');
    document.getElementById("txtFNF").innerHTML = "<br>" + val + "<br><br>";
    for (let index = 1; index <= value.length; index++) {
        if (Number(value[value.length - index]) == 1) {
            document.getElementById("txtFNF").innerHTML += index - 1 + " - " + FNFtoString(index - 1) + "<br>";
        }
    }
</script>

让值='12345678';
设val=value.match(/.{1,4}/g).join(“”,’);
document.getElementById(“txtFNF”).innerHTML=“
”+val+”

”; 对于(让index=1;索引不带修剪的可能重复项
“0000000000000000”。替换(/.{4}\B/g,“$&”)