我想结合字符串
我的输入是
"the, quick, brown, 'fox, name fred', jumps"
我想要的输出是
the quick browm 'fox, name fred' jumps
JavaScript代码
var a = "the, quick, brown, 'fox, name', jumps".split(",").join(" ");
for (var i = 0; i < a.length; i++)
{
document.write(a[i]);
}
当我使用document.write时我得到输出但是当我使用console.log时,一切都搞砸了可以有人帮助我了
我的输出是
t,
h,
e,
q,
u,
I,
答案 0 :(得分:1)
请尝试以下操作:
var input = "the, quick, brown, 'fox, name fred', jumps";
var output = input.replace(/,/g, "");
更新:OP提到他想要排除单引号中的逗号被替换。
var input = "the, quick, brown, 'fox, name fred', jumps";
//Replace all the commas except for the ones in single quotes.
var strings = input.split(/'/g);
var newStrings = [];
for(var i=0;i<strings.length;i++) {
if(i%2 === 0)
newStrings[i] = strings[i].replace(/,/g, "");
else
newStrings[i] = strings[i];
}
console.log(newStrings.join('\''));