White space trim for JavaScript

I originally blogged about this on my other site lazygnome.net but that was simply a quick place to record it.

I present to you a very quick and handy way of replicating PHP’s trim() function in JS:

function ws_trim(value)
  return value.replace(/(^s+|s+$)/g, '');

There it is, simple and usable.

I don’t claim to be original with this code. I’m sure that others have done exactly the same themselves. But it surprises me that JS doesn’t come with such a text manipulation method by default.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.