Remove all <H tags

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
jollyjumper
Forum Contributor
Posts: 107
Joined: Sat Jan 25, 2003 11:03 am

Remove all <H tags

Post by jollyjumper »

Hello Everyone,

I'm trying to have all <H*>(where * is a number) tags to be removed from my html document with RegExp.

I've got this expression:

Code: Select all

var re = new RegExp("(<H1)(&#1111;^>]*>.*?)(<\/H1>)","gi") ;	html = html.replace( re, '$2' ) ;
But this keeps the last > of <H1> and only works with H1 tags, and I would like to let it work on all H tags.

I hope some one could explain me how to do this.

Greetz Jolly..
evilMind
Forum Contributor
Posts: 145
Joined: Fri Sep 19, 2003 10:09 am
Location: Earth

Post by evilMind »

in php you can do:

Code: Select all

$stringToReplaceTagsIn = ereg_replace( "<(h|H)[0-9]>",'',$stringToReplaceTagsIn);
$stringToReplaceTagsIn = ereg_replace( "<\/(h|H)[0-9]>",'',$stringToReplaceTagsIn);
OR even better

Code: Select all

$stringToReplaceTagsIn = ereg_replace("<(h|H)[0-9]>",'',ereg_replace( "<\/(h|H)[0-9]>",'',$stringToReplaceTagsIn));
jollyjumper
Forum Contributor
Posts: 107
Joined: Sat Jan 25, 2003 11:03 am

Post by jollyjumper »

Hi evilMind,

Thank you very much for your reply, it helped me very much.

This is what it looks like in javascript.

Code: Select all

var text = "<H1>line1</H1>\n\n<H2>line2</H2>";
text = text.replace(/<(h|H)&#1111;0-9]>/gi,"");
text = text.replace(/<\/(h|H)&#1111;0-9]>/gi,"");
Again thank you.

Greetz Jolly.
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

javascript borrows from perl. thus g = global and i = canse insensitive, and you can use the short \d
thus you can change it to /<h\d>/gi

just some notes for the future. you might wanna pick up a book on perl regular expressions or the learning perl book by oreilly press. there's a great reg exp section there (3 chapters on it) and you can learn the shorts.
jollyjumper
Forum Contributor
Posts: 107
Joined: Sat Jan 25, 2003 11:03 am

Post by jollyjumper »

Hi m3rajk,

Thanks for the book reference. I'll certainly take a look at it.

Greetz Jolly.
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

since i have learning perl, here's the isbn: 0596001320

i wish i had stock in bookpool. it's a comp.. mayby sci book specialty store on the web that keeps coming up with the lowest prices for me, and i have to include sales tax when going to them.

http://www.bookpool.com

there's 3 chapters on regular expressions, and if you are interested in perl anyway, then why not get your ref via that book?
Post Reply