[英]String replace “<br>↵” with commas in javascript array
當我將數組打印到控制台時,我得到以下符號:“ <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>\↵/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);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.