3

I'm trying to make a discord bot in js, that will write to the minecraft white-list. When I read whitelist.txt and use string.split ("\n"), for example, a name is created in the field: Name\r. But I don't want this there, did I forget something? Thank you

const names = fs.readFileSync("C:\\Users\\Admin\\Desktop\\pack\\whitelist.txt", {
  encoding: 'utf8'
});

var jmena = names.replace('\r', "").split("\n");

console.log(jmena);
demo
  • 5,676
  • 16
  • 65
  • 141
Romis Pejs
  • 77
  • 3

1 Answers1

1

\r is a carriage return character, and the backslash \ escapes whatever character is after it. So the replace function is looking for a carriage return, but the string would be \\r.

So for you to escape the backslash \ with another backslash.

var jmena = names.replace('\\r', "").split("\n");
a.mola
  • 3,730
  • 7
  • 23