M
Markus
Hello
While working on some white space trimming methods I found that the "Non
breaking space" character (ASCII 160, nbsp, \u00A0) is treated
differently accross browsers. A short test shows: MSIE and Safari do
treat it as non-whitespace, FF and Opera treat it as whitespace.
This is the test code (the space between "hel" and "lo" consists of
three nbsp characters, typed on Windows with ALT + 160):
var teststr = "hel lo";
alert (teststr.replace(/\s+/g, ""));
- MSIE and Safari alert "hel lo"
- Firefox and Opera alert "hello"
I found some discussions about this. My personal oppinion is that nbsp
should be treated as non-whitespace, for 2 reasons:
- In HTML rendering it is treated like this (every single occurrence of
the character is rendered, unless other whitespace which is collapsed to
one space); treating it differently in Javascript regular expressions is
not consistent
- There are easy ways to have nbsp treated along with whitespace (as
shown in the trim function provided in this group's FAQ,
http://www.jibbering.com/faq/index.html#FAQ4_16), but if nbsp is treated
as whitespace by default, it is less trivial to get it preserved, if
this behaviour is desired.
Now, this is actually my question - the expected behaviour in my case is
to preserve non breaking spaces. Is there a possibility to exclude the
white space character explicitly from the \s class, or do I have to
replace \s by an enumeration of all whitespace characters? Such as:
alert (teststr.replace(/[ \f\n\r\t\v]+/g, ""));
Thanks for your comments!
While working on some white space trimming methods I found that the "Non
breaking space" character (ASCII 160, nbsp, \u00A0) is treated
differently accross browsers. A short test shows: MSIE and Safari do
treat it as non-whitespace, FF and Opera treat it as whitespace.
This is the test code (the space between "hel" and "lo" consists of
three nbsp characters, typed on Windows with ALT + 160):
var teststr = "hel lo";
alert (teststr.replace(/\s+/g, ""));
- MSIE and Safari alert "hel lo"
- Firefox and Opera alert "hello"
I found some discussions about this. My personal oppinion is that nbsp
should be treated as non-whitespace, for 2 reasons:
- In HTML rendering it is treated like this (every single occurrence of
the character is rendered, unless other whitespace which is collapsed to
one space); treating it differently in Javascript regular expressions is
not consistent
- There are easy ways to have nbsp treated along with whitespace (as
shown in the trim function provided in this group's FAQ,
http://www.jibbering.com/faq/index.html#FAQ4_16), but if nbsp is treated
as whitespace by default, it is less trivial to get it preserved, if
this behaviour is desired.
Now, this is actually my question - the expected behaviour in my case is
to preserve non breaking spaces. Is there a possibility to exclude the
white space character explicitly from the \s class, or do I have to
replace \s by an enumeration of all whitespace characters? Such as:
alert (teststr.replace(/[ \f\n\r\t\v]+/g, ""));
Thanks for your comments!