Tip Details Add a Related Tip

Rate as: Positive Negative

Trimming whitespace in Javascript

Unlike most programming languages, Javascript does not have a built-in method to allow trimming white space off of strings. If you want to perform trimming of white space in Javascript, here are some functions you can use to perform this functionality.

function trimString(inString) {
    // Trims space from the left and right sides of the string
    return inString.replace(/^\s+|\s+$/g, "");
}

function trimStringLeft(inString) {
    // Trims space from the left side of a string
    return inString.replace(/^\s+/g, "");
}

function trimStringRight(inString) {
    // Trims space from the right side of a string
    return inString.replace(/\s+$/g, "");
}
Rating: 100% positive, 2 total Votes
Categories: javascript programming
Added: on Sep 13, 2007 at 1:54 pm
Added By: an anonymous user

Comments on this Tip

Add a Comment
There are no comments for this tip
Your Comment:
(how to format)
Rate This Tip:

Verify Humanity:
Sorry, we know it's annoying, but please enter the characters shown in the image to the left so that we know you're an actual person and not an evil spammer. Thanks.
       

Related Tips

Using optional parameters in Javascript functions

Categories: javascript programming
100% positive, 3 comments - last added on May 05, 2008 at 6:41 am
– Tip added by an anonymous user on Apr 19, 2007 at 11:43 am

Static variables in Javascript

100% positive, 2 comments - last added on May 08, 2008 at 4:19 pm
– Tip added by an anonymous user on Aug 03, 2007 at 4:19 pm

Using ASP.Net variables in your Javascript code

100% positive, 2 comments - last added on Sep 04, 2007 at 7:12 am
– Tip added by an anonymous user on May 05, 2007 at 7:00 am

Scrolling HTML elements using the keyboard

100% positive, 0 comments
– Tip added by rlansky on Jun 22, 2007 at 9:34 am

Great AJAX repository

100% positive, 0 comments
– Tip added by hindy on May 02, 2007 at 7:23 am

Javascript Error: unterminated regular expression literal

100% positive, 0 comments
– Tip added by marty on Jun 26, 2008 at 1:20 pm

compressing javascript

100% positive, 0 comments
– Tip added by an anonymous user on Jun 12, 2008 at 3:02 pm

Telling when an iframe is done loading

100% positive, 0 comments
– Tip added by an anonymous user on Apr 18, 2008 at 8:05 am

Creating vector graphics in javascript

100% positive, 0 comments
– Tip added by Marcos84 on May 10, 2007 at 2:44 pm

Javascript: How to Create a Random Number

0% positive, 0 comments
– Tip added by Walkere on Feb 23, 2008 at 9:49 pm

Javascript variable naming

no ratings, 0 comments
– Tip added by an anonymous user on Sep 30, 2007 at 7:28 am

Code for creating Javascript CSS charts

no ratings, 0 comments
– Tip added by an anonymous user on Aug 21, 2007 at 12:23 pm

Efficient building of large strings in Javascript

no ratings, 0 comments
– Tip added by an anonymous user on Aug 08, 2007 at 12:52 pm

Avoid invalid title names when opening javascript windows

no ratings, 0 comments
– Tip added by an anonymous user on Aug 07, 2007 at 9:36 am

Nice article on javascript techniques

no ratings, 0 comments
– Tip added by rlansky on Apr 24, 2007 at 8:12 am

Minimize round trips to the server for best performance

100% positive, 0 comments
– Tip added by an anonymous user on Jun 18, 2008 at 4:07 pm

1067 errors in Actionscript

no ratings, 0 comments
– Tip added by an anonymous user on Jun 17, 2008 at 9:28 pm

Referencing methods in PHP usort

Categories: php coding programming
no ratings, 0 comments
– Tip added by an anonymous user on Jun 13, 2008 at 11:18 am

CakePHP: Working with Associations

no ratings, 0 comments
– Tip added by an anonymous user on May 08, 2008 at 4:09 am

Creating your own Quick Look Plug-Ins

no ratings, 0 comments
– Tip added by an anonymous user on Apr 29, 2008 at 7:34 am