Prev: Replacing text on the fly
Next: mo better qooxlisp
From: FAQ server on 5 Jun 2010 19:00 ----------------------------------------------------------------------- FAQ Topic - How do I trim whitespace? ----------------------------------------------------------------------- ECMAScript 5 defines ` String.prototype.trim `. Where not supported, it can be added as a function that uses a regular expression: if(!String.prototype.trim) { String.prototype.trim = function() { return String(this).replace(/^\s+|\s+$/g, ""); }; } Implementations are inconsistent with ` \s `. For example, some implementations, notably JScript 5.8 and Safari 2, do not match ` \xA0 ` (no-break space), among others. A more consistent approach would be to create a character class that defines the characters to trim. https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/RegExp http://thinkweb2.com/projects/prototype/whitespace-deviations/ https://developer.mozilla.org/en/Firefox_3.1_for_developers http://docs.sun.com/source/816-6408-10/regexp.htm http://msdn.microsoft.com/en-us/library/6wzad2b2%28VS.85%29.aspx http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/c7010139217600c3/31092c5eb99625d0?#31092c5eb99625d0 http://unicode.org/Public/UNIDATA/PropList.txt The complete comp.lang.javascript FAQ is at http://jibbering.com/faq/ -- The sendings of these daily posts are proficiently hosted by http://www.pair.com.
From: Dr J R Stockton on 7 Jun 2010 08:11 In comp.lang.javascript message <4c0ad6fc$0$286$14726298(a)news.sunsite.dk >, Sat, 5 Jun 2010 23:00:03, FAQ server <javascript(a)dotinternet.be> posted: >----------------------------------------------------------------------- >FAQ Topic - How do I trim whitespace? >----------------------------------------------------------------------- > >ECMAScript 5 defines ` String.prototype.trim `. Where not supported, >it can be added as a function that uses a regular expression: Surely 'trim' is a Method, and therefore "as" should be "with". >if(!String.prototype.trim) { >String.prototype.trim = function() { >return String(this).replace(/^\s+|\s+$/g, ""); >}; >} > >Implementations are inconsistent with ` \s `. For example, >some implementations, notably JScript 5.8 and Safari 2, do not match ` \xA0 ` >(no-break space), among others. Safari 2 is somewhat /pass�/. Of my browsers, only MS IE 8 (JScript 5.8.22960) fails to see \u00A0 as a space. Perhaps someone can test a more recent Mobile Safari with <URL:http://www.merlyn.demon.co.uk/js-valid.HTM#RsT>. -- (c) John Stockton, nr London UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME. Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links. Proper <= 4-line sig. separator as above, a line exactly "-- " (RFCs 5536/7) Do not Mail News to me. Before a reply, quote with ">" or "> " (RFCs 5536/7)
|
Pages: 1 Prev: Replacing text on the fly Next: mo better qooxlisp |