当我将数组打印到控制台时,我得到以下符号:“<br>↵
”
如何用JS数组中的逗号替换“<br>↵
”的所有实例?
e.g。
["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() );
});
答案 0 :(得分:4)
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);
});