Regex question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris

    Regex question

    I need to write a function that will remove a specified parameter from a
    URL. For example:

    removeParam("pa ram1", "http://mysite.com/mypage.htm?para m1=1&param2=2") ;

    would return:

    "http://mysite.com/mypage.htm?para m2=2"

    I'm thinking that string.replace(/regex/, ""); would do the trick, but how
    do I construct a correct regex?

    I see a problem if the parameter name ("param1") happens to contain any
    characters that have a special meaning in a regular expression.


  • Mick White

    #2
    Re: Regex question

    Chris wrote:
    [color=blue]
    > I need to write a function that will remove a specified parameter from a
    > URL. For example:
    >
    > removeParam("pa ram1", "http://mysite.com/mypage.htm?para m1=1&param2=2") ;
    >
    > would return:
    >
    > "http://mysite.com/mypage.htm?para m2=2"[/color]

    This would be a candidate for string manipulation rather than using reg
    ex. But Reg ex is something like the following:

    function removeParam(par am,url){
    regex=eval("/&?"+param+"=[^&]+&/")
    return url.replace(reg ex,'')
    }

    This should work with any number of queries, characters inside square
    brackets don't need to be escaped, I think.

    Mick
    [color=blue]
    >
    > I'm thinking that string.replace(/regex/, ""); would do the trick, but how
    > do I construct a correct regex?
    >
    > I see a problem if the parameter name ("param1") happens to contain any
    > characters that have a special meaning in a regular expression.
    >
    >[/color]

    Comment

    • Grant Wagner

      #3
      Re: Regex question

      Mick White wrote:
      [color=blue]
      > Chris wrote:
      >[color=green]
      > > I need to write a function that will remove a specified parameter from a
      > > URL. For example:
      > >
      > > removeParam("pa ram1", "http://mysite.com/mypage.htm?para m1=1&param2=2") ;
      > >
      > > would return:
      > >
      > > "http://mysite.com/mypage.htm?para m2=2"[/color]
      >
      > This would be a candidate for string manipulation rather than using reg
      > ex. But Reg ex is something like the following:
      >
      > function removeParam(par am,url){
      > regex=eval("/&?"+param+"=[^&]+&/")[/color]

      eval() is evil().

      <url: http://jibbering.com/faq/#FAQ4_40 />

      Especially when there is a RegExp object constructor that lets you do the same
      thing.

      var regex = new RegExp("&?" + param + "=[^&]+&");

      Also "var" keyword added to avoid scope issues on the "regex" variable.
      [color=blue]
      > return url.replace(reg ex,'')
      > }
      >
      > This should work with any number of queries, characters inside square
      > brackets don't need to be escaped, I think.
      >
      > Mick[/color]

      --
      | Grant Wagner <gwagner@agrico reunited.com>

      * Client-side Javascript and Netscape 4 DOM Reference available at:
      *


      * Internet Explorer DOM Reference available at:
      *
      Find official documentation, practical know-how, and expert guidance for builders working and troubleshooting in Microsoft products.


      * Netscape 6/7 DOM Reference available at:
      * http://www.mozilla.org/docs/dom/domref/
      * Tips for upgrading JavaScript for Netscape 7 / Mozilla
      * http://www.mozilla.org/docs/web-deve...upgrade_2.html


      Comment

      • Mick White

        #4
        Re: Regex question

        Grant Wagner wrote:
        [color=blue]
        > Mick White wrote:[/color]

        [color=blue][color=green]
        >>This would be a candidate for string manipulation rather than using reg
        >>ex. But Reg ex is something like the following:
        >>
        >>function removeParam(par am,url){
        >>regex=eval( "/&?"+param+"=[^&]+&/")[/color]
        >
        >
        > eval() is evil().[/color]

        I disagree, especially when "param" is unknown, and may contain escaped
        characters.

        Mick
        [color=blue]
        > <url: http://jibbering.com/faq/#FAQ4_40 />
        >
        > Especially when there is a RegExp object constructor that lets you do the same
        > thing.
        >
        > var regex = new RegExp("&?" + param + "=[^&]+&");
        >
        > Also "var" keyword added to avoid scope issues on the "regex" variable.
        >
        >[color=green]
        >>return url.replace(reg ex,'')
        >>}
        >>
        >>This should work with any number of queries, characters inside square
        >>brackets don't need to be escaped, I think.
        >>
        >>Mick[/color]
        >
        >
        > --
        > | Grant Wagner <gwagner@agrico reunited.com>
        >
        > * Client-side Javascript and Netscape 4 DOM Reference available at:
        > *
        > http://devedge.netscape.com/library/...ce/frames.html
        >
        > * Internet Explorer DOM Reference available at:
        > *
        > http://msdn.microsoft.com/workshop/a...ence_entry.asp
        >
        > * Netscape 6/7 DOM Reference available at:
        > * http://www.mozilla.org/docs/dom/domref/
        > * Tips for upgrading JavaScript for Netscape 7 / Mozilla
        > * http://www.mozilla.org/docs/web-deve...upgrade_2.html
        >
        >[/color]

        Comment

        Working...