Thursday, November 3, 2011

String trimming in Javascript

Here is a simple trim function to remove leading and trailing whitespaces.

function trim(s) {
return s.replace(/^\s*|\s*$/g, '');
}

and in CoffeeScript
trim = (s) ->
s.replace /^\s*|\s*$/g, ''


You can check Steven Levithan's collection of trim functions.

No comments:

Post a Comment