RegExp Question

M

Matt

Premise: Inserting data into a database form a <textarea> and
displaying this data through an HTML page.

When inserting data into a database via a text area and then displaying
this data any carriage return that was entered into the database was
lost. I Created a function to replace a carriage return with a <BR>. I
call this function when I display this data.

If I enter text inside <> into a database and then display this data,
any text that was within the <> is lost because it is considered an
HTML tag. How can I search the string for the brackets and actually
diplay them? I do not want to use Server.HTMLEncode before I insert the
data into the database. This is because we often query the database
from outside a web browser.

Here is my function I have for the carriage return. I need to figure
out how to accomplish this for both < and >. I am sure this is simple
but I do not know what the .pattern should be (for a carriage return it
is "\r".

SET regQuote = New RegExp
regQuote.pattern = "\r"
regQuote.global = True
catchCR = regQuote.Replace(strText, "<br>")

Thanks in Advance. I will post this to an ASP and a VBScript Group.
 
E

Evertjan.

Matt wrote on 27 jan 2006 in microsoft.public.inetserver.asp.general:
I will post this to an ASP and a VBScript Group.

Do not multipost, better crosspost.

Aswering a question and later finding that the same Q was answered elswhere
does not encourage. Netquette rules have a reason.

Crossposting = specifying more than one NG,
so that the thread runs common to both NGs [or more].
 
A

Anthony Jones

Matt,

You should use Server.HTMLEncode when displaying the contents of the field.

Use that first then perform your replace function.

It's wierd that you call this a RegExp question.

It's wierder still that you used RegExp at all


Dim sField ' contains value retrieved from DB.

sField = Server.HTMLEncode(sField)
sField = Replace(Replace(sField, vbCR, ""), vbLF, "<br />")


Note I kill all CRs then replace LFs. Depending other technologies you may
be using (one of note is XML) CRs can go missing leaving just LFs. LFs are
always there. The above code robustly handles this situation if it arises.


Anthony.
 
D

Dave Anderson

Matt said:
Premise: Inserting data into a database form a <textarea> and
displaying this data through an HTML page.

When inserting data into a database via a text area and then
displaying this data any carriage return that was entered
into the database was lost...

The carriage return is not lost when you store it in the DB. You will still
see it in your HTML source when attempting to display it.
...How can I search the string for the brackets and actually
diplay them? I do not want to use Server.HTMLEncode before I
insert the data into the database...

The usual way of dealing with this is to use Server.HTMLEncode when
injecting into an HTML stream. You can follow this with your replace
function to achieve both goals:

JScript:
<div><%=Server.HTMLEncode(val).replace(/\r\n/g,"<br>")%></div>

VBScript:
<% Set rx = New RegExp : rx.Pattern = vbCrLf : rx.Global = True %>
<div><%=rx.Replace(Server.HTMLEncode(val),"<br>")%></div>

Keep in mind that you do not need to replace your carriage returns when
injecting back into a textarea:

<textarea><%=Server.HTMLEncode(val)%></textarea>



--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms. Please do not contact
me directly or ask me to contact you directly for assistance. If your
question is worth asking, it's worth posting.
 
H

Hal Rosser

Matt said:
Premise: Inserting data into a database form a <textarea> and
displaying this data through an HTML page.

When inserting data into a database via a text area and then displaying
this data any carriage return that was entered into the database was
lost. I Created a function to replace a carriage return with a <BR>. I
call this function when I display this data.

If I enter text inside <> into a database and then display this data,
any text that was within the <> is lost because it is considered an
HTML tag. How can I search the string for the brackets and actually
diplay them? I do not want to use Server.HTMLEncode before I insert the
data into the database. This is because we often query the database
from outside a web browser.

Here is my function I have for the carriage return. I need to figure
out how to accomplish this for both < and >. I am sure this is simple
but I do not know what the .pattern should be (for a carriage return it
is "\r".

SET regQuote = New RegExp
regQuote.pattern = "\r"
regQuote.global = True
catchCR = regQuote.Replace(strText, "<br>")

Thanks in Advance. I will post this to an ASP and a VBScript Group.

I think VB uses chr(11) and chr(13) rather than the escape sequences like /r
or /n.
So look for chr(11) and replace it with a break tag seems like the thing to
do.
 
E

Evertjan.

Hal Rosser wrote on 28 jan 2006 in
microsoft.public.inetserver.asp.general:
I think VB uses chr(11) and chr(13) rather than the escape sequences
like /r or /n.
So look for chr(11) and replace it with a break tag seems like the
thing to do.

Not chr(11)

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

VbCr = chr(13) ' carriage return

VbLf = chr(10) ' line feed

VbCrLf = chr(13) & chr(10) ' both

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

In the old teletype [called telex in Europe] days
the mechanical carriage return took so long,
that the extra time given by sending the line feed at 45.45 baud
was also important not to miss the next printable character.

That's why there is VbCrLf and not VbLfCr.
 
H

Hal Rosser

I think VB uses chr(11) and chr(13) rather than the escape sequences
Not chr(11)

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

VbCr = chr(13) ' carriage return

VbLf = chr(10) ' line feed

VbCrLf = chr(13) & chr(10) ' both

You're right - I was going by memory and should have looked it up first.
My point is that '/r' may work for javascript, but not for VB, that chr(n)
may be the course to take, but as you point out, vbcrlf works as well.
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top