tags:

views:

68

answers:

4

Can someone help me understand this commit

What is \u00A0 ?

+6  A: 

The code strips trailing and leading space characters in addition to non breaking spaces, \u00A0 is the unicode point/code/number/alias for the non breaking space. You can pop up Firebug and type it out.

>>> '\u00A0'
" "

Further information about this character:

http://www.fileformat.info/info/unicode/char/00a0/index.htm

Edit: I just tried this code in Firebug, which checks for any matches from the generated non breaking space and it matched:

javascript:alert( String.fromCharCode(160).match(/\s/) )

However, upon doing this in IE6 it returns null, so IE is most likely the reason for this update.

Update #2 - it looks like the removal of the caret makes this inconsistent with the previous version, because it removes not just leading and trailing spaces but spaces anywhere.

r = /(\s|\u00A0)+|(\s|\u00A0)+$/g;

s = '  wh  at'

s = s.replace( r, '' )

Outputs

"what"
meder
Although the ECMAScript standard includes `\u00a0` in `\s`, IE does not appear to.
Annabelle
Here is the link which says 160 is the unicode for non breaking white space. http://wikisource.org/wiki/Transwiki:Unicode_characters_160-191
Roger
@Roger - my first link had that information.
meder
A: 

Looks like a Unicode space character which is NOT equivalent or equal to \u0020.

Stefan Kendall
+1  A: 

I'd guess that it's the same as String.fromCharCode(160) from the test case - a non-breaking space. It's making sure that trim also trims off non-breaking space characters, which may not normally match the \s character class.

developmentalinsanity
+1  A: 

It is the code point for the non-breaking space character in Unicode. The commit seems to work around a bug in Internet Explorer's JavaScript implementation where the \s regular expression character class does not include \u00a0.

Annabelle