Dave's Brain

Browse - programming tips - javascript trim space

Date: 2010mar26
Language: javaScript

A.  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 + '<');
	}
What this info useful to you? You can donate to say thanks

Add a comment

Sign in to add a comment
Copyright © 2008-2012, 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.
Advertisements: