By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,291 Members | 3,096 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,291 IT Pros & Developers. It's quick & easy.

RegExp Question

P: n/a
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.

Jan 27 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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].

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jan 27 '06 #2

P: n/a
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.
Jan 27 '06 #3

P: n/a
Matt wrote:
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.
Jan 27 '06 #4

P: n/a

"Matt" <ma***********@manning-napier.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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.
Jan 28 '06 #5

P: n/a
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.
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jan 29 '06 #6

P: n/a
> > 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


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.
Feb 1 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.