Dave's Brain

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 + '<'); }

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.