当我将数组打印到控制台时,我得到以下符号:“<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/