Welcome to WebmasterWorld Guest from 54.226.147.190

Forum Moderators: open

Message Too Old, No Replies

[regexp] RegExp not working in IE

     

fm86

11:07 am on Jul 15, 2011 (gmt 0)

5+ Year Member



Hi everybody!

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?

Thanks a lot!

penders

12:17 pm on Jul 15, 2011 (gmt 0)

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



Hi fm86,

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)
<div id="myText">--Hello--World</div>


So, in IE, when you read the value from #myText you are actually getting something a bit different:
Hello-World

...so your RegExp is failing.

EDIT:
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]

fm86

12:48 pm on Jul 15, 2011 (gmt 0)

5+ Year Member



Hello and thanks for your reply.

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 "&nbsp;" So maybe this is messing things up even more... But I have no clue how to obtain the desired result.

penders

12:54 pm on Jul 15, 2011 (gmt 0)

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



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.

penders

3:13 pm on Jul 15, 2011 (gmt 0)

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



Besides, the " " is actually stored as "&nbsp;" 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 "&nbsp;" 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.
 

Featured Threads

Hot Threads This Week

Hot Threads This Month