White-space and character 160

I came across a problem in Internet Explorer (it wasn't a problem with Firefox) when I was trying to compare two strings. To me, one string looked to have an extra space in the front. No problem, I'll just call the jQuery trim function . Well, that didn't work. So I used charCodeAt and found it was ASCII character 160. I looked up char code 160 and saw that it is a "Non-breaking space". You would think that a "space" character would be trimmed. I looked at the jQuery code that does the trimming and the grep pattern uses \s. So, evidently you can't use \s to catch the "Non-breaking space" in IE. I wonder why no one else has seen this. I wrote up a test page to illustrate this.

To create the non-breaking space, you can use String.fromCharCode(160) or the Unicode representation "\u00A0". " " doesn't seem to work when using regular expressions, although it behaves the same when printed.

Browser Evaluates character 160 as white-space
Firefox 3, 3.5Yes
Chrome 3.0Yes
Internet Explorer 7, 8No
Safari 3.2No
Opera 9Yes

13 thoughts on “White-space and character 160

  1. You saved me hours of debugging in ie. I was using .innerHTML to insert a php page with a table, and i kept getting a runtime error. I spend a good 2 hours debugging only to finally see your post about you having that error when inserting a form within a form. I looked and indeed the DIV displaying the php table had a form around it. Thanks a lot dude!!! 🙂

  2. damnit 3 years later and I still run into this bug (with chrome btw)
    it looks like a contenteditable uses 160 as a whitespace in chrome (/webkit?)
    thanks for your insight

  3. dude I ran into the same problem on chrome. I was comparing the text came from text area to white space character (normal ascii 32) and guess what ??? !!!
    some of the spaces where 32 and others where 160
    Thank you for investigating this .. I think you will save so much time and effort for many Devs

  4. Thanks for posting this. (7 years later) I ran into this with Mulesoft (Java based) parsing engine. Copy and paste out of browser based displays caused this.

  5. Same in Excel – sheets created opened from an HTML source also have this issue. Excel doesn't find/replace programmatically when searching for a space in vba, but will when using the GUI.

  6. Editing an HTML page originally created with Microsoft Word, I stripped out all the crap and converted all the inserted special characters to real characters, or in some cases to named HTML characters like é (lower-case e with an acute accent). When all that was done, there were still a few random   codes, which I deleted because they served no purpose.

    So I guess it's not just Microsoft Internet Explorer, but Microsoft Word as well. If you actually NEED an extra space somewhere that won't go away, the HTML characters is   "non-breaking space."

    • Argh! Didn't expect the HTML codes to be expressed in my comment. My bad. It should read "HTML characters like é" and "the HTML character is  ".

  7. I have been trying to pull data from an excel file downloaded off chrome into another excel report using vlookups on names and could not get it to work. Thank you so much for highlighting this difference – turns out my data is riddled with 160s and 32s making them initially non-comparable. You have saved me many hours of troubleshooting!

  8. FYI I just ran into this in Java. Character.isWhitespace returns false for 160, but Character.isSpaceChar returns true.

  9. So glad I found this article, have spent an age trying to figure out why my Vue component Unit test was failing. Turns out what I thought was a space was… yes you guessed it a Code 160 non-breaking space.

  10. Some one else too…

    I have formatting and converting currency rates… And strangely Hungary (country) uses this character as their number group (hundreds) separator and came across this strange character.

  11. Just what I was looking for! Ran into this problem with Javascript. Was comparing the formatted numeric values for different locales with the value entered by the user. This was to implement a search function in my application. Finally was frustrated and checked using the charCodeAt() function. And lo! There it was! Will have to explicitly handle this case now.