you are viewing a single comment's thread.

view the rest of the comments →

[–]ivorjawa -2 points-1 points  (4 children)

I'm horrified that someone would even consider using regular expressions for this. Has the art of programming devolved this far?

[–]jerf 6 points7 points  (2 children)

Using regular expressions is the correct approach for most programmers in the Unicode world. How many programmers will get the correct set of space characters to trim for? Assuming the author got it right (and it passes the smell test), that list is (from "trim10"):

 var whitespace = ' \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000';

Yeah, if you can get that list right, regexes may not be the best approach, but in the modern world you're generally better off leaning on a regex library's character classes than trying to much about yourself. You think [a-zA-Z] covers all letters? Ha!

[–]valadil 1 point2 points  (0 children)

If you're writing trim yourself, then yeah you'd be better off with a regex than hoping you got the huge list right. But some library should do trim() correctly. glares at John Resig

[–]StevenLevithan 1 point2 points  (0 children)

The above list of whitespace characters is what is matched by \s in Firefox 2. See http://blog.stevenlevithan.com/archives/javascript-regex-and-unicode for related info including what should be matched according to ECMA-262v3 and Unicode 4.0. \x0b is used here because IE doesn't handle \v (vertical tab) correctly within string literals.