Browse - Programming Tips - How can I remove leading and trailing space from a string in javaScript?

Date: 2010mar26
Language: javaScript

Q.  How can I remove leading and trailing space from a string in javaScript?

A.  Here's a simple function that does that:

function trimSpace(str) { return str.replace(/^\s+|\s+$/g, ''); } function exampleUse() { var s; s = " \t hello \t\n"; s = trimSpace(s); alert('>' + s + '<'); }