- 我是 js 新手。
- 我正在尝试编写一个用 0 替换空格的代码。
- 我得到了部分输出,但如何才能喜欢这个“call0cell0”。
- 不确定如何组合,现在我在它们之间添加逗号
- 提供以下代码
var word = "cell call ";
var replaceWord = [];
for (i=0; i < word.length; i++) {
replaceWord.push(word[i]);
if (word[i] === " ") {
replaceWord.pop(word[i] = ' ');
replaceWord.push(word[i] = '0');
}
}
console.log("result----->" + replaceWord);
最佳答案
<body>
<p id="demo"></p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
var str ="cell call ";
var res = str.replace(/\s\s+/g, ' ').replace(' ', '0');
document.getElementById("demo").innerHTML = res;
}
</script>
</body>
关于javascript - - 不确定如何组合,现在我在它们之间加上逗号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43566839/