字符串在JavaScript数组中用逗号替换“ <br>↵”

皮姆

将数组打印到控制台时,出现以下符号:“ <br>↵

如何<br>↵在JS数组中用逗号替换“ ”的所有实例

例如

["767 5th Ave<br>↵New York, NY 10153, USA", "677 5th Ave<br>↵New York, NY 10022, USA"]

["767 5th Ave, New York, NY 10153, USA", "677 5th Ave, New York, NY 10022, USA"]

该值取自:

<address class="main">
  <p>767 5th Ave<br>
  New York, NY 10153, USA</p>
</address>

使用以下代码:

$("address.main p:not(:empty)").each(function() {
  addresses_google[0].push( $(this).html() );
});
卡尔·爱德华兹

var addresses = ["767 5th Ave<br>↵New York, NY 10153, USA", "677 5th Ave<br>↵New York, NY 10022, USA"];

var formattedAddresses = addresses.map(function(str) {
  return str.replace(/<br>\u21b5/g, ", ");
});

console.log(formattedAddresses);

更新:

似乎是将html用换行符(解释为↵符号,用于可视化换行符)与纯字符串文字推入此数组。

$("address.main p:not(:empty)").each(function() { 
  addresses_google[0].push( $(this).html() );
});

现在我们知道这是循环的结果,我们可以在该循环的代码块中完成所有操作,而无需创建新数组:

$("address.main p:not(:empty)").each(function() {
  var breaks = /<br>(\r\n|\n|\r)/gm;
  var formattedHTML = this.innerHTML.replace(breaks, ', ');

  addresses_google[0].push(formattedHTML);
});

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章