homepage Welcome to WebmasterWorld Guest from 54.161.133.166
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
[regexp] RegExp not working in IE
fm86




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

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




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

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




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

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




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

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




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

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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved