How to remove line breaks from a string in Node.js

2 Answers

0 votes
// utils/string.js
function removeNewLines(input) {
  return input.replace(/(\r\n|\n|\r)/g, "");
}
// module.exports = { removeNewLines };

// index.js
//const { removeNewLines } = require('./utils/string');

let str = 'c#\n javascript\n c++ \n c \n typescript \r node.js\n';
 
str = removeNewLines(str);
 
console.log(str);

 
 
 
/*
run:
 
c# javascript c++  c  typescript  node.js
 
*/

 



answered Feb 18, 2022 by avibootz
edited Feb 21 by avibootz
0 votes
// utils/string.js
function removeNewLines(input) {
  return input.replace(/(\r\n|\n|\r)/g, "").replace(/\s+/g, " ") .trim();
}
// module.exports = { removeNewLines };

// index.js
//const { removeNewLines } = require('./utils/string');

let str = 'c#\n javascript\n c++ \n c \n typescript \r node.js\n';
 
str = removeNewLines(str); // result without extra spaces
 
console.log(str);
 
 
 
 
/*
run:
 
c# javascript c++ c typescript node.js
 
*/

 



answered Feb 21 by avibootz
...