【问题标题】:removing linebreak from array elements Javascript从数组元素Javascript中删除换行符
【发布时间】:2015-07-09 22:48:05
【问题描述】:

我有以下数组,声明为“var strArry”:

 <h4>  ``</h4>,<h4><font color="DarkSalmon">"We</font></h4>,<h4> wo </h4>,<h4><font color="ForestGreen">"'t comment on the</font></h4>,<h4>m . '' </h4>

它给出以下 5 个字符串的输出,用逗号分隔:

``,"We, wo ,"' 不对,m 发表评论。 ''

然后我使用以下命令调用数组以形成一个不带逗号的连续字符串:

var htmlElementsforSrcLanguageSentence = htmlElementsforSrcLanguageSentence +strArry.join("");

下面给出的输出不是我需要的:

 ``
"We
wo
"'t comment on the
m . '' 

这是按预期删除逗号,但也将每个元素放在一个新行中。这不起作用,因为我需要将其显示为一个句子。我错过了什么?

【问题讨论】:

    标签: javascript arrays line-breaks


    【解决方案1】:

    您为什么不直接使用 .toString() ,然后从那里执行 .replace() 以将逗号替换为空格?

    var newArray     = new Array("item1", "item2", "item3", "item4");
    var newString    = newArray.toString();
    var editedString = newString.replace(/,/g, " ");
    console.log(editedString);
    

    编辑 1:

    如果您在 javascript 控制台中运行它,它会执行您想要的操作,我会为您准备一个 jsfiddle。

    编辑 2:

    Here is the JS Fiddle

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-21
      • 2010-10-17
      • 2010-09-21
      相关资源
      最近更新 更多