I get the following symbol when I print my array to console: "<br>↵
"
How do I replace all instances of "<br>↵
" with commas in a JS array?
e.g.
["767 5th Ave<br>↵New York, NY 10153, USA", "677 5th Ave<br>↵New York, NY 10022, USA"]
to
["767 5th Ave, New York, NY 10153, USA", "677 5th Ave, New York, NY 10022, USA"]
The value is taken from:
<address class="main">
<p>767 5th Ave<br>
New York, NY 10153, USA</p>
</address>
with the following code:
$("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);
Update:
It appears that there's html being pushed into this array with line breaks (interpreted as ↵ symbols, used to visualize a newline) vs. plain string literals.
$("address.main p:not(:empty)").each(function() {
addresses_google[0].push( $(this).html() );
});
Now that we know this is the result of a loop, we could do this all within that loop's code block without the need of even creating a new array:
$("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);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With