String strip() for JavaScript?

Go To StackoverFlow.com

161

What's a clean and efficient JavaScript implementation to strip leading and trailing spaces from a string?

For example:

" dog"

"dog "

" dog "

" dog "

all get turned into

"dog"

2009-09-13 15:52
by rawrrrrrrrr
This function is rathern known as trim. See https://developer.mozilla.org/En/CoreJavaScript1.5Reference/GlobalObjects/String/Tri - Gumbo 2009-09-13 15:57
@Gumbo: trim is a bit newer, and may not be supported across browsers. Don't know, but you can never tell with cross browser support - David Andres 2009-09-13 16:10


190

Use this:

if(typeof(String.prototype.trim) === "undefined")
{
    String.prototype.trim = function() 
    {
        return String(this).replace(/^\s+|\s+$/g, '');
    };
}

The trim function will now be available as a first-class function on your strings. For example:

" dog".trim() === "dog" //true

EDIT: Took J-P's suggestion to combine the regex patterns into one. Also added the global modifier per Christoph's suggestion.

Took Matthew Crumley's idea about sniffing on the trim function prior to recreating it. This is done in case the version of JavaScript used on the client is more recent and therefore has its own, native trim function.

2009-09-13 15:57
by David Andres
see http://blog.stevenlevithan.com/archives/faster-trim-javascript for some performance dat - Christoph 2009-09-13 16:35
to be compatible with the ECMA spec, trim() has to cast this to type string, ie you'll have to change this.replace(...) to String(this).replace(...) or ('' + this).replace(...); this allows to call() or apply() the function to non-string value - Christoph 2009-09-13 19:14
@Christoph: do you mean to say within the String.prototype.trim() = ... function definition? I would have expected "this" to already refer to a string, as this is a prototypal function extending the String type - David Andres 2009-09-13 19:44
@David: yes, within the function; the ECMA spec allows trim() to be called on non-string objects, eg String.prototype.trim.apply(42) or MyObj.prototype.trim = String.prototype.trim; new MyObj().trim();Christoph 2009-09-13 19:57
@Christoph, can we get by by 42.toString() or 42 + "" - David Andres 2009-09-13 20:10
yes, but if you want to conform to the spec, you'll have to put the conversion into trim() (as I already explained 3 comments ago ;)); also, it's better to use String(42) instead of 42.toString() as the former won't create an unneeded wrapper object and is the canonical way to cast to type strin - Christoph 2009-09-13 20:24
@David: otherwise, String.prototype.trim.apply(foo) will throw an error if foo doesn't have a replace() method; the spec allows such a use: "The trim function is intentionally generic; it does not require that its this value be a String object. Therefore, it can be transferred to other kinds of objects for use as a method. - Christoph 2009-09-13 21:03
@Christoph: I believe the only way for the "this" instance to not have a replace method, or at least to have a different replace method, would be due to user intervention (assigning a different function to the instance.replace property). In this case, I understand your point. I just want to be sure I understand your statement though, because I can't understand how a String would need to casted back to a String for any other reason than what I've written here - David Andres 2009-09-13 21:10
Note that /\s/ will not strip all whitespace characters defined by ES3 spec. I recently wrote about the way browsers handle /\s/ - http://thinkweb2.com/projects/prototype/whitespace-deviations/ - and how to work around it - kangax 2009-09-13 21:21
Curiously enough the original two-regex version (replace(/^\s+/, '').replace(/\s+$/, '');) is actually slightly faster in many browsers, so you shouldn't choose the one-regex version out of performance concerns. Pick whichever you find more readable - bobince 2009-09-14 01:47
@bobince: This makes sense. Alternation probably slows down the regex engine somewhat and there might be a lack of optimization for it - David Andres 2009-09-14 12:24
Why is /\b\s+|\s+\b/g not working - most venerable sir 2017-06-11 17:41
@user132522: are you trying to collapse excess whitespace into a single space with that pattern? If so, what text does it not work against - David Andres 2017-06-28 05:27
I got it thank - most venerable sir 2017-06-29 15:45


139

For jquery users, how about $.trim(s)

2011-02-28 21:05
by user638373


81

Gumbo already noted this in a comment, but this bears repeating as an answer: the trim() method was added in JavaScript 1.8.1 and is supported by all modern browsers (Firefox 3.5+, IE 9, Chrome 10, Safari 5.x), although IE 8 and older do not support it. Usage is simple:

 "  foo\n\t  ".trim() => "foo"

See also:

2012-01-17 01:41
by jpatokal
This should arguably be the top answer - Nitrodist 2019-02-24 08:40


11

Here's the function I use.

function trim(s){ 
  return ( s || '' ).replace( /^\s+|\s+$/g, '' ); 
}
2009-09-13 16:08
by Mic


11

A better polyfill from the MDN that supports removal of BOM and NBSP:

if (!String.prototype.trim) {
  String.prototype.trim = function () {
    return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
  };
}

Bear in mind that modifying built-in prototypes comes with a performance hit (due to the JS engine bailing on a number of runtime optimizations), and in performance critical situations you may need to consider the alternative of defining myTrimFunction(string) instead. That being said, if you are targeting an older environment without native .trim() support, you are likely to have more important performance issues to deal with.

2014-10-20 10:42
by Bardi Harborow


7

Steven Levithan once wrote about how to implement a Faster JavaScript Trim. It’s definitely worth a look.

2009-09-13 17:14
by Gumbo


7

If you're already using jQuery, then you may want to have a look at jQuery.trim() which is already provided with jQuery.

2012-01-23 12:04
by Filip Dupanović


6

If, rather than writing new code to trim a string, you're looking at existing code that calls "strip()" and wondering why it isn't working, you might want to check whether it attempts to include something like the prototypejs framework, and make sure it's actually getting loaded.
That framework adds a strip function to all String objects, but if e.g. you upgraded it and your web pages are still referring to the old .js file it'll of course not work.

2010-08-16 16:34
by Eric
exactly why I came here - momeara 2013-11-17 20:42
Ads