filter html-tags, but not all...

L

Lasse Edsvik

Hello

I was wondering if you guys could help me. im building a small messageboard
and i want to filter all html-tags except these ones:

<b> <i> <img.........> <font....>

how to build such a reg exp pattern?

TIA
/Lasse
 
R

Ray at

I ~guess~ this non-regular expression may work:


sPost = Request.Form("TheTextArea")
sPost = Replace(sPost, "<", "&lt;")
sPost = Replace(sPost, "&lt;b>", "<b>")
sPost = Replace(sPost, "&lt;/b>", "</b>")
sPost = Replace(sPost, "&lt;i>", "<i>")
sPost = Replace(sPost, "&lt;/i>", "</i>")
sPost = Replace(sPost, "&lt;img", "<img")
sPost = Replace(sPost, "&lt;font", "<font")
sPost = Replace(sPost, "&lt;/font>", "</font>")


If I got that right, it'll replace all the <'s with &lt;'s, and then go back
and re-replace the <b, <i, <img, <font and they're closing equivalents.
This will still leave > everywhere, but that shouldn't matter. I'm sure
there is a more robust solution out there, anyway.

Ray at work
 
B

Bob Barrows

Lasse said:
hmm, i was hoping for a reg.exp solution
Patience. Chris (Hohmann) usually doesn't start posting this early in the
day. ;-)

If you're really impatient, you may want to Google this group for some of
Chris' RegExp posts that may help you figure it out for yourself...

Bob Barrows
 
E

Evertjan.

Lasse Edsvik wrote on 27 okt 2003 in
microsoft.public.inetserver.asp.general:
I was wondering if you guys could help me. im building a small
messageboard and i want to filter all html-tags except these ones:

<b> <i> <img.........> <font....>

how to build such a reg exp pattern?

This answer is in [serverside-asp ;-)] Jscript
and supposes you also want to keep </b>, </i>, etc.


// replace the < of those 8 with ##&##
t = t.replace(/<{1}?(?=\/?(b|i|img|font)( |>))/g,"##&##")

// replace all other tags with a space
t = t.replace(/<[^>]+>/g," ")

// replace all ##&## back to <
t = t.replace(/##&##/g,"<")

=========================

// all in one single line:
t = t.replace(/<{1}?(?=\/?(b|i|img|font)( |>))/g,"##&##").replace(/<[^>]
+>/g," ").replace(/##&##/g,"<")
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,054
Latest member
LucyCarper

Latest Threads

Top