DelphiFAQ Home Search:

How can I trim a string in JavaScript?

 

comments43 comments. Current rating: 5 stars (15 votes). Leave comments and/ or rate it.

Question:

I need to trim a string from white space at the front and end. How can I do that elegantly?

Answer:

Add the following string prototype function to your code. You will call it like predefined String member functions (such
as indexOf(), charAt() and Substring().

The code below shows the function declaration and how to use it.

This implementation of a trim() function uses two regular expressions to first replace the white space at the begiinning,
then the white space at the end of the string. White space in regular expressions is described as \s.
The beginning of the string is matched by ^ (see the first regex) and the end is matched by $ - in the second regex.

// implementing a trim function for strings in javascript

<script language="JavaScript" type="text/javascript">
<!--
String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

var s = new String(" Hello ");
// use it like this
s=s.trim();
alert("!" + s + "!");

// end hiding contents -->
</script>


Comments:

You are on page 3 of 3, other pages: 1 2 [3]
2009-02-21, 07:03:07
anonymous from Belarus  
THANKS SO MUCH!
2009-03-17, 13:56:07
Skews,Me from United States  
rating
I've encountered a problem where MSIE uses \r\n at line breaks while Firefox only inserts the \n

Since I'm splitting the lines a \n how can trim() be updated to remove the \r at the end of the line?

I haven't tested trim() with \t but I'd like to remove them too.
2009-03-20, 00:10:17
Skews,Me from United States  
rating
String.prototype.trim = function()
{
return this.replace(/^\s*/,'').replace(/\s*$/,'').replace(/\r*$/,'');
}

This updated version will remove any \r carriage return symbol at the end of a data line inserted by MSIE. I use it after a data = data.split('\n'); call to turn the data into individual lines for parsing.
2009-04-14, 03:06:54
anonymous  
Excellent
2009-05-08, 01:13:59
anonymous from India  
d
2009-05-11, 07:22:14
anonymous from India  
it's very good
2009-07-03, 08:12:53
anonymous from India  
rating
Thanks dude. Saved a lot of my R&D
2009-07-14, 03:44:28
anonymous from India  
rating
Great Code love it!!!!
2009-08-03, 14:30:49
hm from Bulgaria  
it is not working if the string contains /
2009-08-11, 08:03:21
anonymous from United States  
rating
thank for the code.
2009-08-28, 01:02:16
anonymous from India  
rating
realyy helpfull. thanks
2009-10-09, 01:54:24
anonymous from India  
2009-10-15, 02:32:58
anonymous from India  
Thanks!!!
You are on page 3 of 3, other pages: 1 2 [3]

 

 

NEW: Optional: Register   Login
Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option, or post under a registered account.
 

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:
photo Add a picture:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity. The owner of this web site reserves the right to delete such material.