One line replace for multiple targets?

Discussion in 'ASP General' started by Noozer, Feb 3, 2006.

  1. Noozer

    Noozer Guest

    Writing some ASP code and I need to take a string, representing a phone
    number, and strip out some characters, namely spaces, brackets and hyphens.

    I could write

    pNum=replace(pNum," ","")
    pNum=replace(pNum,"-","")
    pNum=replace(pNum,"(","")
    pNum=replace(pNum,")","")

    ....but that's a bit of a pain. Especially since I'll be doing similar
    replacements with other data and may have to strip out a larger number of
    characters.

    I know that this can be done with regular expressions, but all the examples
    I see online are several lines in length. Isn't there a simple way to just
    say "remove any of these" or "replace any of these with nothing" in a single
    line of code?
    Noozer, Feb 3, 2006
    #1
    1. Advertising

  2. Noozer wrote:
    > Writing some ASP code and I need to take a string, representing a
    > phone number, and strip out some characters, namely spaces, brackets
    > and hyphens.
    > I could write
    >
    > pNum=replace(pNum," ","")
    > pNum=replace(pNum,"-","")
    > pNum=replace(pNum,"(","")
    > pNum=replace(pNum,")","")
    >
    > ...but that's a bit of a pain. Especially since I'll be doing similar
    > replacements with other data and may have to strip out a larger
    > number of characters.
    >
    > I know that this can be done with regular expressions, but all the
    > examples I see online are several lines in length. Isn't there a
    > simple way to just say "remove any of these" or "replace any of these
    > with nothing" in a single line of code?

    No
    Sorry
    --
    Microsoft MVP - ASP/ASP.NET
    Please reply to the newsgroup. This email account is my spam trap so I
    don't check it very often. If you must reply off-line, then remove the
    "NO SPAM"
    Bob Barrows [MVP], Feb 3, 2006
    #2
    1. Advertising

  3. Noozer

    McKirahan Guest

    "Noozer" <> wrote in message
    news:2xDEf.315369$tl.211704@pd7tw3no...
    > Writing some ASP code and I need to take a string, representing a phone
    > number, and strip out some characters, namely spaces, brackets and

    hyphens.
    >
    > I could write
    >
    > pNum=replace(pNum," ","")
    > pNum=replace(pNum,"-","")
    > pNum=replace(pNum,"(","")
    > pNum=replace(pNum,")","")
    >
    > ...but that's a bit of a pain. Especially since I'll be doing similar
    > replacements with other data and may have to strip out a larger number of
    > characters.
    >
    > I know that this can be done with regular expressions, but all the

    examples
    > I see online are several lines in length. Isn't there a simple way to just
    > say "remove any of these" or "replace any of these with nothing" in a

    single
    > line of code?


    If you only want digits then here's one approach.

    <html>
    <head>
    <title>numbonly.htm</title>
    <script type="text/javascript">
    function numbonly(that) {
    var numb = that.value;
    var what = "";
    for (var i=0; i<numb.length; i++) {
    var byte = numb.substr(i,1);
    if (byte >= "0" && byte <= "9") what += byte;
    }
    that.value = what;
    }
    </script>
    </head>
    <body>
    <center>
    <form name="form1">
    <input type="text" name="numb" size="20" maxlength="20"
    value="(123) 456-7890" onblur="numbonly(this)">
    </form>
    </center>
    </body>
    </html>
    McKirahan, Feb 3, 2006
    #3
  4. >>
    I know that this can be done with regular expressions, but all the examples
    I see online are several lines in length. Isn't there a simple way to just
    say "remove any of these" or "replace any of these with nothing" in a single
    line of code?
    <<

    As already stated no there isn't but yet there is; use a function (the art
    of abstraction is often
    lost in the serial nature of ASP script).

    Have the following function (and a range of other useful utilities) in an
    ASP page that is included in the pages where you need them:-

    Function RegExReplace(rsInput, rsPattern, rsReplace)

    Dim oRegEx

    Set oRegEx = New RegExp

    oRegEx.Pattern = rsPattern
    oRegEx.Global = True
    RegExReplace = oRegEx.Replace(rsInput, rsReplace)

    End Function

    Now your stated case becomes:-

    pNum = RegExReplace(pNum;" |-|\)|\)","")

    and your other similar cases will be simply a matter of varing the pattern.


    PS. where is 'microsoft.public.inetserver.iis.activeserverpages' that you
    cross posted to?
    my newsgroup app complains that is doesn't exist

    Anthony.
    Anthony Jones, Feb 4, 2006
    #4
  5. Noozer

    McKirahan Guest

    "Anthony Jones" <> wrote in message
    news:#hI#...

    [snip]

    > PS. where is 'microsoft.public.inetserver.iis.activeserverpages' that

    you
    > cross posted to? my newsgroup app complains that is doesn't exist


    "microsoft.public.inetserver.iis.activeserverpages"
    exists and has had 588 posts since January 1, 2004.
    McKirahan, Feb 4, 2006
    #5
  6. Noozer

    Andyza Guest

    Bob, it's very rare, but this time you're wrong. See the message in
    this thread by Anthony Jones. A Regular Expression is ideal for this
    problem.
    Andyza, Feb 16, 2006
    #6
  7. Andyza wrote:
    > Bob, it's very rare, but this time you're wrong. See the message in
    > this thread by Anthony Jones. A Regular Expression is ideal for this
    > problem.


    Oh wait! I AM wrong. Nested Replace statements will accomplish this on a
    single line.

    This
    > pNum=replace(pNum," ","")
    > pNum=replace(pNum,"-","")
    > pNum=replace(pNum,"(","")
    > pNum=replace(pNum,")","")


    can be done by:
    pnum=replace(replace(replace(replace(pNum,")",""),"(",""),"-","")," ","")

    --
    Microsoft MVP - ASP/ASP.NET
    Please reply to the newsgroup. This email account is my spam trap so I
    don't check it very often. If you must reply off-line, then remove the
    "NO SPAM"
    Bob Barrows [MVP], Feb 16, 2006
    #7
  8. Andyza wrote:
    > Bob, it's very rare, but this time you're wrong. See the message in
    > this thread by Anthony Jones. A Regular Expression is ideal for this
    > problem.

    What are you talking about? Please quote the message to which you are
    replying.
    Oh wait, I remember now.

    A Regular Expession may be ideal for this problem, but a regular expression
    also takes at least 3 lines to use, one to instantiate the regexp object,
    one to initialize the pattern, and one to execute the replace statement. So
    it is not the answer to the question you asked.

    --
    Microsoft MVP - ASP/ASP.NET
    Please reply to the newsgroup. This email account is my spam trap so I
    don't check it very often. If you must reply off-line, then remove the
    "NO SPAM"
    Bob Barrows [MVP], Feb 16, 2006
    #8
  9. Noozer

    Evertjan. Guest

    Noozer wrote on 03 feb 2006 in microsoft.public.inetserver.asp.general:

    > Writing some ASP code and I need to take a string, representing a
    > phone number, and strip out some characters, namely spaces, brackets
    > and hyphens.
    >
    > I could write
    >
    > pNum=replace(pNum," ","")
    > pNum=replace(pNum,"-","")
    > pNum=replace(pNum,"(","")
    > pNum=replace(pNum,")","")
    >
    > ...but that's a bit of a pain. Especially since I'll be doing similar
    > replacements with other data and may have to strip out a larger number
    > of characters.
    >
    > I know that this can be done with regular expressions, but all the
    > examples I see online are several lines in length. Isn't there a
    > simple way to just say "remove any of these" or "replace any of these
    > with nothing" in a single line of code?


    execute function in vbs or js:
    <%
    pNum = pNumReplace(pNum)
    %>

    define in jscript:
    <script runat=server language=jscript>
    function pNumReplace(x) { return x.replace(/[ -()]+/g,'') }
    </script>


    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
    Evertjan., Feb 16, 2006
    #9
  10. Noozer

    Andyza Guest

    > Now your stated case becomes:-
    >
    > pNum = RegExReplace(pNum;" |-|\)|\)","")
    >
    > and your other similar cases will be simply a matter of varing the pattern.


    Maybe I'm reading it wrong, but shouldn't that be:

    pNum = RegExReplace(pNum," |-|\(|\)","")

    i.e.: " |-|\(|\)" instead of " |-|\)|\)"

    The original example has two closing brackets instead of one opening
    and one closing bracket.

    And shouldn't there be a comma after pNum instead of a semicolon?
    Andyza, Feb 16, 2006
    #10

  11. >i.e.: " |-|\(|\)" instead of " |-|\)|\)"


    >The original example has two closing brackets instead of one opening
    >and one closing bracket.


    >And shouldn't there be a comma after pNum instead of a semicolon?


    Your quite right thats a typo.

    Should read:-

    pNum = RegExReplace(pNum," |-|\(|\)","")


    Another option would be:-


    Function CharReplace(rsInput, rsChars, rsReplace)

    Dim i

    CharReplace = rsInput

    For i = 1 To Len(rsChars)
    CharReplace = Replace(CharReplace, Mid(rsChars, i, 1), rsReplace)
    Next

    End Function

    pNum = CharReplace(pNum, " -()", "")

    Personally I prefer the RegExp solution since I don't like to create a
    destory strings if I can help it. But the above may perform better for
    short rsChars.

    My main point is that there is nearly all ways a single line solution.
    Create a function.

    Anthony.
    Anthony Jones, Feb 16, 2006
    #11
    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. Jos
    Replies:
    1
    Views:
    1,027
    PeterMcC
    Dec 16, 2003
  2. Anita Meyer

    Two targets of one form

    Anita Meyer, Jun 30, 2004, in forum: HTML
    Replies:
    15
    Views:
    1,424
    Karl Groves
    Jul 7, 2004
  3. starlight

    One link two targets possible?

    starlight, Mar 7, 2008, in forum: HTML
    Replies:
    6
    Views:
    858
    Beauregard T. Shagnasty
    Mar 7, 2008
  4. gc
    Replies:
    21
    Views:
    511
    Roy Smith
    Aug 18, 2011
  5. Tyrone Slothrop

    One Form, Two Targets, Two Actions

    Tyrone Slothrop, Dec 28, 2006, in forum: Javascript
    Replies:
    5
    Views:
    158
    Jonas Raoni
    Dec 29, 2006
Loading...

Share This Page