IE Javascript Regex bug Fails Jquery trim

If you are using $.trim(astring) in IE, even in IE8, the leading and trailing spaces are not trimmed at all.

This is due to a bug of javascript regex implementation by ie not including \xa0 into \s.

The code of jquery trim is

trim: function( text ) {
		return (text || "").replace( /^\s+|\s+$/g, "" );

As it is rightly pointed out by jquery trim api page, in ie you have to do like this to trim spaces:

astring.replace(/(^[\s\xA0]+|[\s\xA0]+$)/g, '');

I am afraid this is not the only place we will get the problem, in fact all regex ‘\s’ will fail in IE.


This entry was posted on Monday, August 9th, 2010 at 4:21 am and is filed under Javascript. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply