Over a million developers have joined DZone.

Javacript Trim A String

·
removes all leading and trailing spaces from string


str.replace(/^\s*/, "").replace(/\s*$/, "");


alternative, extend the string class

String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); }

Topics:

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}