Sometimes, we want to remove line breaks from start and end of string with JavaScript.
In this article, we’ll look at how to remove line breaks from start and end of string with JavaScript.
How to remove line breaks from start and end of string with JavaScript?
To remove line breaks from start and end of string with JavaScript, we can use the string replace
method.
For instance, we write
const newwStr = str.replace(/^\s+|\s+$/g, "");
to call replace
to remove line breaks from the start and end of the string by replacing them with empty strings.
^\s+
matches the line break at the start of the string.
\s+$
matches the line break at the end of the string.
Conclusion
To remove line breaks from start and end of string with JavaScript, we can use the string replace
method.