[JavaScript] Remove Leading and Trailing Whitespaces
This post is a brief summary of links in the references.
Remove (Trim) Leading Whitespace (similar to Python lstrip)
function lstrip(str) {
return str.replace(/^\s+/g, "");
}
Remove (Trim) Trailing Whitespace (similar to Python rstrip)
function rstrip(str) {
return str.replace(/\s+$/g, "");
}
Remove (Trim) Leading and Trailing Whitespace (similar to Python strip)
function strip(str) {
return str.replace(/^\s+|\s+$/g, "");
}
References:
[1] | Trimming a string in JavaScript |
[2] | trim - String strip() for JavaScript? - Stack Overflow |
[3] | Faster JavaScript Trim |
[4] | String.prototype.trim() - JavaScript | MDN |
[5] | trim Method (String) (JavaScript) |
[6] | JavaScript RegExp s Metacharacter - W3Schools |