Using the javaScript replace() method in IE.

I have a form that has draggable elements and droppable areas.  When a thumbnail sized draggable element is placed within its associate droppable area, the script replaces the thumbnail image with a html comment, and it changes the primary image's display attribute from none to block.  The replace '_t' is necessarily redundant for a case which is not being addressed here.

Processing is managed @ two points w/in the script:
1)   initial processing
var primaryContent = "#".replace('#', ui.draggable.html().replace('_t','').replace('display: none;','display: block;'));

Open in new window

2)   this is the simplist of three conditional statements
var primaryContent = primaryContent.replace(/img.*>/, '!--image removed-->');

Open in new window



It seems as if IE ignores/fails to replace anything.  The included code is my debug output [console.log('');] from both Firefox and Internet Explorer.

Have any of you dealt with anything like this in the past?  Is IE not compatible with the replace() method?  (I am testing in version 8)
From Console (Firefox)
<!--image removed-->
<div style="display: block; margin: 0px; padding: 0px;">
	<span style="font-family: Arial;">
		<img contentelementid="32623" src="http://my.qdobamail.com/userfiles/34/e3d71ab3-3795-102c-9fa9-4fc2de14c7ac/images/mainImages_600x400/6961-football-650x400.jpg" alt="" height="400" width="650">
		<br>
	</span>
</div>

From Console (Internet Explorer)
<IMG style="WIDTH: 50px; HEIGHT: 31px; OVERFLOW: hidden" class=contentElement alt=32623 src="/userfiles/34/3038/32623.jpg"> 
<DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: none; PADDING-TOP: 0px"><SPAN style="FONT-FAMILY: Arial"><IMG alt="There's more to explore TM at&#13;&#10;BEAVERS football games.&#13;&#10;Qdoba now has three locations at Reser Stadium. The Club Level,&#13;&#10;The North Side, and our new location in the South End Zone Corridor.[icon]" src="http://my.qdobamail.com/userfiles/34/e3d71ab3-3795-102c-9fa9-4fc2de14c7ac/images/mainImages_600x400/6961-football-650x400.jpg" width=650 height=400 contentElementID="32623"><BR></SPAN></DIV>

Open in new window

LVL 1
brianmfallsAsked:
Who is Participating?
 
imantasConnect With a Mentor Commented:
As I've told previously, Internet Explorer converts all the tags to UPPERCASE. img is converted IMG and because your regullar expression is case-sensitive, it doesn't match your img tags. You should use case-isensitive search as I have also mentioned previously.

var primaryContent = primaryContent.replace(/img.*>/i, '!--image removed-->');

Open in new window


Please note the letter "i" added after your regullar expression. Also try to combine it with global flag in case it doesn't work:

var primaryContent = primaryContent.replace(/img.*>/gi, '!--image removed-->');

Open in new window


Now two letters ("g" and "i") are added which forces global and case-isensitive search.

Does that help?
0
 
GeoffHarperCommented:
You might try putting /findme/g around all the search strings.  "g" is for global and it's a Regular Expression syntax to replace all occurences of "findme"
0
 
imantasCommented:
Try this:
var primaryContent = primaryContent.replace(/img[^>]*>/g, '!--image removed-->');

Open in new window


But please note that this will replace as many images as there are in the content, not just the first one.
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
brianmfallsAuthor Commented:
Sorry guys, not ignoring this...  There is an error in IE that is keeping me from getting to where this issue was noted that has to be corrected before I can verify anything.  I'll get to it as soon as I am able.  Thanks so much for your input, and again, I apologize for the delay.
0
 
brianmfallsAuthor Commented:
Unfortunately, adding global to the search expression did not change the final render.
0
 
GeoffHarperCommented:
add "gi" instead of just "g"
0
 
imantasCommented:
Because IE convert the tags to uppercase, you should also add case-isensitivity flag:
var primaryContent = primaryContent.replace(/img[^>]*>/gi, '!--image removed-->');

Open in new window

0
 
brianmfallsAuthor Commented:
Alright.  Lets backtrack a bit.  Global isn't an option.  The first instance of the image tag needs to be replaced with a comment.

var primaryContent = primaryContent.replace(/img.*>/, '!--image removed-->');

Open in new window


So <img attribute="value" attribute="value" /> is changed to <!--image removed-->  The above code works in Firefox, Chrome, and Safari.  It fails in Internet Explorer.  Why is it failing, and what do I do to correct it?
0
 
GeoffHarperCommented:
how about trying the below:


var pattern = new RegExp("<img([^<>+]*)>", "gi");
primaryContent = primaryContent.replace(pattern, '<!--image removed-->');

Open in new window

0
 
brianmfallsAuthor Commented:
It's the 'i'...  I don't know what I did that it didn't work the first time I tried it, but it worked well enough the second time!  Thanks everybody.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.