1
r = r.replace(/<TR><TD><\/TD><\/TR>/gi, rider_html);

...does not work in IE but works in all other browsers.

Any ideas or alternatives?

alex
  • 479,566
  • 201
  • 878
  • 984
jhanifen
  • 4,441
  • 7
  • 43
  • 67
  • 1
    What does `r` look like? – alex Jul 02 '11 at 09:33
  • 1
    What does *"does not work in IE"* mean? What exactly "does not work"? Using regular expressions to process HTML in a **browser** seems... strange. Why not convert the HTML to DOM? It's super easy in a browser... – Felix Kling Jul 02 '11 at 09:36
  • [You cannot parse (X)HTML with regular expressions](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454). And it doesn't make sense to do so in a browser, at that. Just use the DOM (or a wrapper around it as provided by e.g. jQuery). – You Jul 02 '11 at 09:40
  • @You - it does depend on the contents of `r`. If it contains a partial snippet of HTML that doesn't validate, then a DOM wrapper isn't going to be much help whether in the browser or not. – Spudley Jul 02 '11 at 09:50
  • @Spudley: Regular expressions are none the less a bad tool for this. – You Jul 02 '11 at 09:56
  • This whole question is lacking context for what they're really trying to do and what r is. Is this DOM manipulation and the value of "r" came from the DOM in which case there's probably an IE issue with fetching inner HTML from the DOM in IE often doesn't look like you think it will? Or is this just string manipulation and there's a regex gone bad? We'd need to know the value of "r" to help with that. Insufficient information for any reasonable help here. The other answers are just guesses for what the question might mean. I posted an answer based on one of those guesses, then deleted it – jfriend00 Jul 02 '11 at 10:00
  • this is a javascript string of html before it is rendered to the dom – jhanifen Jul 02 '11 at 10:02
  • 1
    @jfanifen. OK, then see my answer below. You/we need to know what the value of r is to know why it isn't matching. – jfriend00 Jul 02 '11 at 10:15
  • So yes it was correct that ie was fetching the html odd, it was putting the then new line . ended up removing the and and it seems to work. – jhanifen Jul 10 '11 at 15:30

2 Answers2

0

You could do something like this:

var rows = document.getElementsByTagName('tr');
for (var i = 0; i < rows.length; i++) {
    var children = rows[i].children;
    if (children.length === 1 && children[0].nodeName.toLowerCase() === 'td') {
        children[0].innerHTML = someHTMLdata
    }
}

Note that this sets the value of the table cell, rather than replacing the whole row. If you want to do something other than this, you'll have to use DOM methods rather than innerHTML and specify exactly what you actually want.

lonesomeday
  • 233,373
  • 50
  • 316
  • 318
0

I've come to the conclusion that the variable r must not have the value in it you expect because the regex replacement should work fine if there is actually a match. You can see in this jsFiddle that the replace works fine if "r" actually has a match in it.

This is the code from fiddle and it shows the proper replacement in IE.

var r = "aa<TR><TD></TD></TR>bb";
var rider_html = " foo ";

r = r.replace(/<TR><TD><\/TD><\/TR>/gi, rider_html);
alert(r);

So, we can't really go further to diagnose without knowing what the value of "r" is and where it came from or knowing something more specific about the version of IE that you're running in (in which case you can just try the fiddle in that version yourself).

If r came from the HTML of the document, then string matching on it is a bad thing because IE does not keep the original HTML around. Instead it reconstitutes it when needed from the parsed page and it puts some things in different order (like attributes), different or no quotes around attributes, different capitalization, different spacing, etc...

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • You shouldn't add table structure with `innerHTML`. – lonesomeday Jul 02 '11 at 09:51
  • Yeah, I generally don't use tables for lots of reason. I've edited my post in a different direction since the original post really just shows a string manipulation, not a DOM manipulation anyway. – jfriend00 Jul 02 '11 at 10:10