Shoutbox

[RESOLVED] JavaScript: Remove <table>blah</table> - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: [RESOLVED] JavaScript: Remove <table>blah</table> (/showthread.php?tid=93701)

[RESOLVED] JavaScript: Remove <table>blah</table> by macgyver08 on 01-29-2010 at 09:32 PM

I've tried...

code:
string.replace(/<table([^%]+)</table>/ig,"");
...but it doesn't seem to be working.
RE: JavaScript: Remove <table>blah</table> by matty on 01-29-2010 at 09:37 PM

Are you trying to replace everything including the <table></table>?


RE: RE: JavaScript: Remove <table>blah</table> by macgyver08 on 01-29-2010 at 09:39 PM

quote:
Originally posted by matty
Are you trying to replace everything including the <table></table>?

Yes, the table tags, and everything they contain.
RE: JavaScript: Remove <table>blah</table> by Spunky on 01-29-2010 at 09:45 PM

Javascript code:
// IGNORE -> html = html.replace(/\<table\>.+?\<\/table\>/gi, "");
html = html.replace(/\<table.+?table\>/gi, "");


Should do it.

EDIT:
Changed it to allow for attributes in the table tag such as styles, class, id etc.
RE: RE: JavaScript: Remove <table>blah</table> by macgyver08 on 01-29-2010 at 09:59 PM

quote:
Originally posted by Spunky
Javascript code:
// IGNORE -> html = html.replace(/\<table\>.+?\<\/table\>/gi, "");
html = html.replace(/\<table.+?table\>/gi, "");


Should do it.

EDIT:
Changed it to allow for attributes in the table tag such as styles, class, id etc.
Doesn't appear to be working *-)

EDIT: I didn't see your edit before i said it didn't work. Let me try your new one.
RE: JavaScript: Remove <table>blah</table> by macgyver08 on 01-29-2010 at 10:03 PM

New one isn't working either. I don't know if it's your code,or my code that I've  pasted it to though.

I replaced the code with one that removes square brackets and that works, so...it seems like it's your code. Not to insult though, I really appreciate the help, it's just not working.


RE: JavaScript: Remove <table>blah</table> by Spunky on 01-29-2010 at 10:13 PM

Yes, it does work. Maybe there is some strange character in the html?

[Image: attachment.php?pid=986406]

EDIT: Try adding the m modifier to make it run over multiple lines... My test was on a single line basis


RE: JavaScript: Remove <table>blah</table> by macgyver08 on 01-29-2010 at 10:18 PM

Well...it's not working with my code for some reason. I don't know why. What strange character in the HTML could stop it from working?

Also, I'm fairly new to JavaScript, so it's quite likely it's something I've done (or haven't done). Everything else is working though. I just need this one line finished and I think it'll be finished lol


RE: JavaScript: Remove <table>blah</table> by Spunky on 01-29-2010 at 10:29 PM

quote:
Originally posted by macgyver08
\<table.+?table\>/gi

I dunno, experiencing a problem with it too now with a more complicated table
RE: JavaScript: Remove <table>blah</table> by Spunky on 01-29-2010 at 10:40 PM

Line breaks are casuing it to break and I can't get the m modifier to work so we can add an extra replace.

Javascript code:
html = html.replace(/\n/gi, "").replace(/\<table.+?table\>/gi, "");


If you wanted to, you could replace the \n with a unique string so it can be put back together again...

Javascript code:
html = html.replace(/\n/gi, "this-used-to-be-a-line-break").replace(/\<table.+?table\>/gi, "");
html = html.replace(/this-used-to-be-a-line-break/gi, "\n");
// This is just so the source formatting looks good when viewed,
// rather than all being on one line


RE: JavaScript: Remove <table>blah</table> by macgyver08 on 01-29-2010 at 11:12 PM

Hey, I found the problem. It was that what I was testing it on was on multiple lines, so I had to use the metacharacters \r and \n.

code:
html.replace(/<table(.|[\r\n])+?<\/table\>/gi, "");

Thanks for your help though! I really appreciate it :D
RE: JavaScript: Remove <table>blah</table> by Spunky on 01-29-2010 at 11:48 PM

Well done, it's a lot more elegant than my solution, but couldn't remember the right syntax for it at the time :p