RegExp Question

Discussion in 'ASP General' started by Matt, Jan 27, 2006.

  1. Matt

    Matt Guest

    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.
    Matt, Jan 27, 2006
    #1
    1. Advertising

  2. Matt

    Evertjan. Guest

    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)
    Evertjan., Jan 27, 2006
    #2
    1. Advertising

  3. 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.
    Anthony Jones, Jan 27, 2006
    #3
  4. 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.
    Dave Anderson, Jan 27, 2006
    #4
  5. Matt

    Hal Rosser Guest

    "Matt" <> wrote in message
    news:...
    > 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.
    Hal Rosser, Jan 28, 2006
    #5
  6. Matt

    Evertjan. Guest

    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)
    Evertjan., Jan 29, 2006
    #6
  7. Matt

    Hal Rosser Guest

    > > 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.
    Hal Rosser, Feb 1, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Greg Hurrell
    Replies:
    4
    Views:
    152
    James Edward Gray II
    Feb 14, 2007
  2. Mikel Lindsaar
    Replies:
    0
    Views:
    467
    Mikel Lindsaar
    Mar 31, 2008
  3. Joao Silva
    Replies:
    16
    Views:
    344
    7stud --
    Aug 21, 2009
  4. Uldis  Bojars
    Replies:
    2
    Views:
    186
    Janwillem Borleffs
    Dec 17, 2006
  5. Matìj Cepl

    new RegExp().test() or just RegExp().test()

    Matìj Cepl, Nov 24, 2009, in forum: Javascript
    Replies:
    3
    Views:
    171
    Matěj Cepl
    Nov 24, 2009
Loading...

Share This Page