How to remove line breaks from a string in TypeScript

2 Answers

0 votes
function removeNewLines(input: string): string {
  return input.replace(/(\r\n|\n|\r)/g, "");
}

const str = 'javascript\n c++ \n c \n typescript \r node.js\n';

const result = removeNewLines(str);

console.log(result);


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

 



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

const str = 'javascript\n c++ \n c \n typescript \r node.js\n';

const result = removeNewLines(str);

console.log(result); // result without extra spaces


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

 



answered Feb 21 by avibootz
...