I’m sure there are a bunch of libraries that do this, but sometimes wrestling with regexes is fun.
> var str = " This string has some mighty fine whitespace. "; > str ' This string has some mighty fine whitespace. ' > str.replace(/^(\s*)((\S+\s*?)*)(\s*)$/,"$2"); 'This string has some mighty fine whitespace.'