Over a million developers have joined DZone.

Javacript Trim A String

DZone's Guide to

Javacript Trim A String

Free Resource
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, ''); }


Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}