I googled but I see only solutions for the particular cases and for I am a newbie with regexp I can't grasp the general solution. So here is the problem:
I want to set var oldText to contain the text value of div #myText where all double occurrences of white spaces are replaced with "". So if I have in #myText a string " hello world" I will have that oldText contains "helloworld". I do as follows:
var regExp = /\s\s/g; var oldText = $("#myText").text().replace(regExp, "");
Works perfectly in FF but in IE it doesn't, meaning that oldText contains " hello world" as no replace were performed. Can someone help me explaining and solving this problem?
The problem is not with your RegExp - that's OK. The problem appears to be with IE and the DOM (Document Object Model). IE has automatically reduced the double spaces to single spaces as stored in the actual DOM (any spaces at the start will have been removed entirely). Other browsers appear to preserve the white-space in the DOM, and only reduce the white-space when it is displayed in the page.
(Using '-' for a single space in this example)
So, in IE, when you read the value from #myText you are actually getting something a bit different:
...so your RegExp is failing.
Works perfectly in FF but in IE it doesn't, meaning that oldText contains " hello world" as no replace were performed.
You say that oldText contains " hello world" in IE (with a preceeding space). Do you have something that preceeds this string in your #myText element? My tests show this should be "hello world" with no preceeding space in IE8. Are you testing in IE8 or a different version?
[edited by: penders at 12:51 pm (utc) on Jul 15, 2011]
This is really weird, I understand that double spaces can be problematic but why is this strange behavior also with --? Besides, the " " is actually stored as " " So maybe this is messing things up even more... But I have no clue how to obtain the desired result.
I understand that double spaces can be problematic but why is this strange behavior also with --?
I used hyphens in the example, just to illustrate the example more clearly. These are spaces in the actual code. It is a lot harder to see the difference between " " (single space) and " " (double space) in the forum.
Besides, the " " is actually stored as " " So maybe this is messing things up even more...
Ah... as it happens, this is an important bit of information (omitted from the OP). This does indeed explain your original result. This would seem to actually help your problem, rather than "messing things up even more".
The browser converts the HTML entity " " in the HTML source into the non-breaking space character (unicode \u00A0). And then the problem is indeed with the RegExp. Whilst all other browsers see the non-breaking space character as ordinary white-space and is captured by the \s RegExp character class, IE does not!
However, you can explicitly check for the non-breaking space in your RegExp and this works in all browsers...
var regExp = /\u00A0\u00A0/g;
This only checks for non-breaking spaces. If you have a mixture of ordinary spaces then you will need to create your own character class.