How do I trim a string in JavaScript?
Join them; it only takes a minute:
Since new Browsers (IE9+) have For those browsers who does not support
See this:
|
|||||||||||||||||||||
|
The trim from jQuery is convenient if you are already using that framework.
I tend to use jQuery often, so trimming strings with it is natural for me. But it's possible that there is backlash against jQuery out there? :) |
|||||||||||||
|
Although there are a bunch of correct answers above, it should be noted that the
Added natively in: JavaScript 1.8.1 / ECMAScript 5 Thus supported in: Firefox: 3.5+ Safari: 5+ Internet Explorer: IE9+ (in Standards mode only!) http://blogs.msdn.com/b/ie/archive/2010/06/25/enhanced-scripting-in-ie9-ecmascript-5-support-and-more.aspx Chrome: 5+ Opera: 10.5+ ECMAScript 5 Support Table: http://kangax.github.com/es5-compat-table/ |
||||
|
There are a lot of implementations that can be used. The most obvious seems to be something like this:
|
|||||||||||||||||||||
|
Simple version here What is a general function for JavaScript trim?
|
|||
|
I know this question has been asked three years back.Now,
|
|||||
|
Flagrant Badassery has 11 different trims with benchmark information: http://blog.stevenlevithan.com/archives/faster-trim-javascript Non-surprisingly regexp-based are slower than traditional loop. Here is my personal one. This code is old! I wrote it for JavaScript1.1 and Netscape 3 and it has been only slightly updated since. (Original used String.charAt)
|
|||||
|
If you are using JQuery use |
|||
|
Use the Native JavaScript Methods:
|
|||||||||||||||||||||
|
Now days you can use string.trim() that is native Javascript implementation
See also |
||||
|
Shamelessly stolen from Matt duereg. |
||||
|
Trim code from angular js project
and call it as |
|||
|
Here's a very simple way:
|
|||
You can just declare your variable as string and use its trim function:
|
||||
|
use simply code
|
|||
|
I have a lib that uses trim. so solved it by using the following code.
|
|||||
|
Today, pretty much every browser supports You use it like this :
In case you still might need to support an ancient browser that doesn't support this feature, this is a polyfill suggested by the MDN :
|
||||
|
Don't know what bugs can hide here, but I use this:
Or this, if text contain enters:
Another try:
|
||||
|
Here it is in TypeScript:
It will fall back to the regex if the native prototype is not available. |
|||
|
I had written this function for trim, when the .trim() function was not available in JS way back in 2008. Some of the older browsers still do not support the .trim() function and i hope this function may help somebody. TRIM FUNCTION
Explanation: The function trim() accept a string object and remove any starting and trailing whitespaces (spaces,tabs and newlines) and return the trimmed string. You can use this function to trim form inputs to ensure valid data to be sent. The function can be called in the following manner as an example.
|
|||
|
You can do it using the plain JavaScript:
Here are some of the more examples to trim a string using JavaScript. |
|||
|
mine uses a single regex to look for cases where trimming is necessary, and uses that regex's results to determine desired substring bounds:
the one design decision that went into this was using a substring to perform the final capture. s/\?:// (make the middle term capturing) and and the replacement fragment becomes:
there's two performance bets I made in these impls:
|
||||
|
For IE9+ and other browsers
|
|||
|
protected by Brad Feb 23 '13 at 5:39
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
String.trim()
also works fine out of the box in Node.js. – Brad Jul 5 '12 at 22:41String.trim()
, the class method, does not exist in ES5/Node.js; instead,String.prototype.trim()
, the instance method, exists. Usage:' foo '.trim()
, notString.trim(' foo ')
. – frontendbeauty Oct 11 '12 at 23:38$.trim(str)
is always available. – Sygmoral Jan 28 '13 at 19:19