{"id":2218,"date":"2012-01-19T11:48:25","date_gmt":"2012-01-19T10:48:25","guid":{"rendered":"http:\/\/www.nivas.hr\/blog\/?p=2218"},"modified":"2012-01-19T12:52:42","modified_gmt":"2012-01-19T11:52:42","slug":"non-breaking-white-space-in-internet-explorer-8-bug-and-how-to-fix-it","status":"publish","type":"post","link":"https:\/\/www.nivas.hr\/blog\/2012\/01\/19\/non-breaking-white-space-in-internet-explorer-8-bug-and-how-to-fix-it\/","title":{"rendered":"Non-breaking white space Internet Explorer 8 JavaScript regexp bug (and how to fix it)"},"content":{"rendered":"<p>While developing jQuery plugin for upcoming bookmarking &#8220;Items in select boxes&#8221; plugin for our <a href=\"http:\/\/nivas.hr\/en\/vudu-cms\" target=\"_blank\">Vudu CMS<\/a><\/p>\n<p><img loading=\"lazy\" class=\"aligncenter size-full wp-image-2232\" title=\"img1\" src=\"http:\/\/www.nivas.hr\/blog\/wp-content\/uploads\/2012\/01\/img1.jpg\" alt=\"\" width=\"400\" height=\"60\" \/><\/p>\n<p>I wrote a simple regexp to strip few characters (pipe, minus, apostrof and white-space) that are added before the actual item.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n function cleanOptionText(txt)\r\n {\r\n return txt.replace(\/^[\\s|'-]+\/, '');\r\n };\r\n<\/pre>\n<p>It worked just fine on FF9 and Chrome but in IE8 only the first pipe (|) was removed. After some debugging I discovered that I have both spaces and non-breaking spaces that should be removed and that in IE8 class shorthand <code>\\s<\/code> (which should include all white space) doesn\u2019t include non-breaking space.<\/p>\n<p>Code for non-breaking space is 0xa0 (dec 160) so regexp should be updated as follows:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nfunction cleanOptionText(txt)\r\n{\r\nreturn txt.replace(\/^[\\s\\xA0|'-]+\/, '');\r\n};\r\n<\/pre>\n<p><!--more--><\/p>\n<p>Also we took opportunity to update our javascript trim functions:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nString.prototype.trim = function()\r\n{\r\nreturn this.replace(\/^[\\s\\xA0]+|[\\s\\xA0]+$\/g,&quot;&quot;);\r\n}\r\n\r\nString.prototype.ltrim = function()\r\n{\r\nreturn this.replace(\/^[\\s\\xA0]+\/g,&quot;&quot;);\r\n}\r\n\r\nString.prototype.rtrim = function()\r\n{\r\nreturn this.replace(\/[\\s\\xA0]+$\/g,&quot;&quot;);\r\n}\r\n<\/pre>\n<p>Here is complete html test file:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n\r\n&lt;!DOCTYPE html PUBLIC &quot;-\/\/W3C\/\/DTD XHTML 1.0 Strict\/\/EN&quot; &quot;http:\/\/www.w3.org\/TR\/xhtml1\/DTD\/xhtml1-strict.dtd&quot;&gt;\r\n&lt;html xmlns=&quot;http:\/\/www.w3.org\/1999\/xhtml&quot;&gt;\r\n  &lt;head&gt;\r\n    &lt;title&gt;nbsp test&lt;\/title&gt;\r\n  &lt;\/head&gt;\r\n  &lt;body&gt;\r\n    &lt;form action=&quot;#&quot;&gt;\r\n      &lt;fieldset&gt;\r\n      &lt;select id=&quot;testselect&quot;&gt;\r\n        &lt;option value=&quot;1&quot;&gt;&amp;nbsp; &amp;nbsp; Some option&lt;\/option&gt;\r\n      &lt;\/select&gt;\r\n      &lt;\/fieldset&gt;\r\n    &lt;\/form&gt;\r\n\r\n    &lt;script type=&quot;text\/javascript&quot;&gt;\r\n      \/*&lt;![CDATA[*\/\r\n\r\n      var txt=document.getElementById('testselect').options[0].text;\r\n\r\n      var analyizeTxt='';\r\n      var l=txt.length;\r\n      for(var i=0; i&lt;l; i++)\r\n      {\r\n        analyizeTxt+= '[ ('+txt.charCodeAt(i)+')=('+txt.charAt(i)+')]';\r\n      }\r\n\r\n      alert(analyizeTxt);\r\n\r\n      var newTxt1 = txt.replace(\/^\\s+\/, '');\r\n      alert('Using only \\\\s = ('+newTxt1+')');\r\n\r\n\r\n      var newTxt2 = txt.replace(\/^[\\s\\xA0]+\/, '');\r\n      alert('Using \\\\s and \\\\xA0= ('+newTxt2+')');\r\n      \/*]]&gt;*\/\r\n    &lt;\/script&gt;\r\n  &lt;\/body&gt;\r\n&lt;\/html&gt;\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>While developing jQuery plugin for upcoming bookmarking &#8220;Items in select boxes&#8221; plugin for our Vudu CMS I wrote a simple regexp to strip few characters (pipe, minus, apostrof and white-space) that are added before the actual item. It worked just fine on FF9 and Chrome but in IE8 only the first pipe (|) was removed&#8230;.<\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1,18],"tags":[],"_links":{"self":[{"href":"https:\/\/www.nivas.hr\/blog\/wp-json\/wp\/v2\/posts\/2218"}],"collection":[{"href":"https:\/\/www.nivas.hr\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.nivas.hr\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.nivas.hr\/blog\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.nivas.hr\/blog\/wp-json\/wp\/v2\/comments?post=2218"}],"version-history":[{"count":21,"href":"https:\/\/www.nivas.hr\/blog\/wp-json\/wp\/v2\/posts\/2218\/revisions"}],"predecessor-version":[{"id":2254,"href":"https:\/\/www.nivas.hr\/blog\/wp-json\/wp\/v2\/posts\/2218\/revisions\/2254"}],"wp:attachment":[{"href":"https:\/\/www.nivas.hr\/blog\/wp-json\/wp\/v2\/media?parent=2218"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nivas.hr\/blog\/wp-json\/wp\/v2\/categories?post=2218"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nivas.hr\/blog\/wp-json\/wp\/v2\/tags?post=2218"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}