1

I have a list that needs to be generated as a CSV. Here it is:

[101, true, P, b, br, 2020], [101, true, P, 20, br, 2020], [101, true, P, b, breach, 2020]

And I wrote this to convert it:

var link = document.createElement('a');
link.id = 'download-csv';
link.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(queries.toString().replaceAll("[","").replaceAll("],","\\r\\n").replaceAll(",",";").trim().replaceAll("]","")));
link.setAttribute('download', 'yourfiletextgoeshere.csv');
document.body.appendChild(link);
document.querySelector('#download-csv').click();

And generated a CSV file is like this:

101; true; P; b; br; 2020\r\n101; true; P; 20; br; 2020\r\n101; true; P; b; br; 2020

Instead of breaking the line, it continues as if \r\n is a string.

TylerH
  • 20,799
  • 66
  • 75
  • 101
dani
  • 25
  • 5
  • Does this answer your question? [How do I create a new line using javascript that shows correctly in notepad?](https://stackoverflow.com/questions/1278501/how-do-i-create-a-new-line-using-javascript-that-shows-correctly-in-notepad) – Heretic Monkey Jan 26 '21 at 19:09

1 Answers1

-1

From the comment by user freedomn-m, which OP responded 'solved' their problem:

Use this:

.replaceAll("],","\\r\\n") -> .replaceAll("],","\r\n")

TylerH
  • 20,799
  • 66
  • 75
  • 101