Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP General > RegExp Question

Reply
Thread Tools

RegExp Question

 
 
Matt
Guest
Posts: n/a
 
      01-27-2006
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.

 
Reply With Quote
 
 
 
 
Evertjan.
Guest
Posts: n/a
 
      01-27-2006
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)
 
Reply With Quote
 
 
 
 
Anthony Jones
Guest
Posts: n/a
 
      01-27-2006
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.


 
Reply With Quote
 
Dave Anderson
Guest
Posts: n/a
 
      01-27-2006
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.


 
Reply With Quote
 
Hal Rosser
Guest
Posts: n/a
 
      01-28-2006

"Matt" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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.


 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      01-29-2006
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)
 
Reply With Quote
 
Hal Rosser
Guest
Posts: n/a
 
      02-01-2006
> > 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
new RegExp().test() or just RegExp().test() Matěj Cepl Javascript 3 11-24-2009 02:41 PM
[regexp] How to convert string "/regexp/i" to /regexp/i - ? Joao Silva Ruby 16 08-21-2009 05:52 PM
Ruby 1.9 - ArgumentError: incompatible encoding regexp match(US-ASCII regexp with ISO-2022-JP string) Mikel Lindsaar Ruby 0 03-31-2008 10:27 AM
Programmatically turning a Regexp into an anchored Regexp Greg Hurrell Ruby 4 02-14-2007 06:56 PM
RegExp.exec() returns null when there is a match - a JavaScript RegExp bug? Uldis Bojars Javascript 2 12-17-2006 09:59 PM



Advertisments