javascript - 字符串用javascript数组中的逗号替换 "<br>↵"

标签 javascript jquery arrays string replace

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

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

例如

["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);
});

关于javascript - 字符串用javascript数组中的逗号替换 "<br>↵",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40985549/

相关文章:

javascript - 我的箭头功能有什么问题?

javascript - 我的响应式菜单出现故障

javascript - 如何修复基于文本输入的 jquery 站点重定向

javascript - 返回用于点击处理程序的 promise

c - 在 main 之前声明数组

java - 检查字符串中每个字符是否有效

javascript - Webpack将 “auto/”添加到内置html文件中的脚本标签中

javascript - 如何使用 sweet.js 编译多个文件?

javascript - 如何在加载和显示没有任何样式的页面后加载样式表

c# - 如何通过比较两个数组来检索不同的字符串值?