For those of you who tried to put together a “flatten” function cross browser to eliminate consecutive spaces and/or trim some text that is retrieved from the HTML page itself, you might have noticed that a simple regex of the kind
/\s+/gm
is simply not enough and occasionally Internet Explorer would still return a string with multiple consecutive space characters! The reason for that is that Internet Explorer does NOT treat
(non-breaking spaces) as a space — and therefore the \s
won’t match it! In order to include this in your match, look at W3C list of HTML entities, and it transpires that
is the same as
— in other words 160 is the decimal code for this character (which is A0 in hex by the way). So to include this when searching for spaces, your regex now becomes:
/(\s|\xA0)+/gm
and you’ll notice that now you “catch” the non-breaking spaces in IE as well as Firefox (which does the right thing and treats the non-breaking spaces as proper spaces) — yahoo!
Yet another IE drawback.Thanks for the post..was going nuts,wondering y IE doesnt remove