470,594 Members | 1,392 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,594 developers. It's quick & easy.

filter html-tags, but not all...

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
Jul 19 '05 #1
4 5006
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

"Lasse Edsvik" <la***@nospam.com> wrote in message
news:OK**************@TK2MSFTNGP12.phx.gbl...
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

Jul 19 '05 #2
hmm, i was hoping for a reg.exp solution

/Lasse
"Ray at <%=sLocation%>" <myfirstname at lane34 dot com> wrote in message
news:uz**************@TK2MSFTNGP11.phx.gbl...
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

"Lasse Edsvik" <la***@nospam.com> wrote in message
news:OK**************@TK2MSFTNGP12.phx.gbl...
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


Jul 19 '05 #3
Lasse Edsvik wrote:
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

--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
Jul 19 '05 #4
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,"<")

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Robin Cull | last post: by
1 post views Thread by Robert Neville | last post: by
8 posts views Thread by dick | last post: by
3 posts views Thread by dhowell | last post: by
4 posts views Thread by Thomas | last post: by
9 posts views Thread by John | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.