Search code snippets, questions, articles...

Remove new line separator from start and end positions of string in Javascript

You can remove line breaks or line separators for a string using the below regex expression in the string replace function in javascript.
my_str = "\n\n Hello World \n\n\n";
my_str = my_str.replace(/^\s+|\s+$/g, '');
console.log(my_str);
Output
Hello World

In the code snippet, we have a string my_str which contains line breaks on its start and end positions and we are removing them from the string.

Search Index Data (The code snippet can also be found with below search text)

remove line breaks from start and end positions of string using javascript
Was this helpful?
0 Comments
Programming Feeds
Learn something new everyday on Devsheet