javascript
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.
Was this helpful?
Similar Posts
- Change color of the line in Chart.js line chart
- Lodash - Create a string from array elements join with some separator
- Remove items begin from start until given condition is false Lodash
- Lodash - Find item index from array by searching from the end
- Batched Requests - AirTable - Automations - Front End - 12/3/21
- Sort Tree - Bloomberg - Port Interactive Analytics - Front End - 12/6/21
- Make y axis to start from 0 in Chart.js