0

I am trying to replace multiple \n in a string but it never replace \n. My same regex works properly in browser but why its not working inside node.js? Thanks.

My string:

var text = '\n\n\n\n\n\n\nYo! this is \n\n\n my string';

All of below regex works in browser, but not in nodejs:

var text = '\n\n\n\n\n\n\nYo! this is \n\n\n my string';

var test1 = text.replace(/[\r\n]{2,}/g, "");
console.log("test 1:", test1);

var test2 = text.trim();
console.log("test 2:", test2);

var test3 = text.replace(/\n+/g, "");
console.log("test 3:", test3);
  • It works for me. Can you show what the outputs you are expecting and the outputs that you are currently seeing in node? Also, you might add ` %j` inside the first string passed to your `console.log()` so that any control characters show up better. – mscdex Sep 01 '15 at 04:47
  • Oh my god, after adding %j I can see its double backslash this is working: `console.log('%j', str.replace(/\\n/g, ''));` Thanks for pointing it out. –  Sep 01 '15 at 04:54

0 Answers0