Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. A question about two regular expression patterns, please help.

A question about two regular expression patterns, please help.

Scheduled Pinned Locked Moved Web Development
regextutorialquestionjavascriptdatabase
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    howardjr
    wrote on last edited by
    #1

    When I append showDialog='Name' to the url location to my page the following statement returns minus 1, for no match.

    document.location.search.search( /(^\?|,)showDialog=(['"])?(Name|Number)(\1)?(,|$)/i )
    

    but the next statement returns zero, for a match starting at the begging of the string.

    document.location.search.search( /(^\?|,)showDialog=(['"])?(Name|Number)(['"])?(,|$)/i )
    

    The difference between the two is that the first one is supposed to match the quotes that enclose the word Name or Number. On page 170 of the O'Reilly book, JavaScript The Definitive Guide, by David Flanagan, a very good book on JavaSrcipt, the following example is shown and works:

    /(['"])[^!"]*\1
    

    This pattern looks for either a single or double quote, followed by any other characters, then the matching single or double quote that was initially found. What I need for there to be an optional initial single or double quote, followed by either Name or Number, then the matching quote that matches the initial single or double quote, if any. This pattern is to occur immediately after the url location's query string's question mark or after that after a comma. The pattern should then end with a following comma or the end of the string. Can any Regular Expression Gurus tell me why the first statement doesn't work and the correct pattern to use? Until then I can use the second expression, understanding that the Name/Number value may not actually be enclosed with matching quotes. Thank you.

    G C 2 Replies Last reply
    0
    • H howardjr

      When I append showDialog='Name' to the url location to my page the following statement returns minus 1, for no match.

      document.location.search.search( /(^\?|,)showDialog=(['"])?(Name|Number)(\1)?(,|$)/i )
      

      but the next statement returns zero, for a match starting at the begging of the string.

      document.location.search.search( /(^\?|,)showDialog=(['"])?(Name|Number)(['"])?(,|$)/i )
      

      The difference between the two is that the first one is supposed to match the quotes that enclose the word Name or Number. On page 170 of the O'Reilly book, JavaScript The Definitive Guide, by David Flanagan, a very good book on JavaSrcipt, the following example is shown and works:

      /(['"])[^!"]*\1
      

      This pattern looks for either a single or double quote, followed by any other characters, then the matching single or double quote that was initially found. What I need for there to be an optional initial single or double quote, followed by either Name or Number, then the matching quote that matches the initial single or double quote, if any. This pattern is to occur immediately after the url location's query string's question mark or after that after a comma. The pattern should then end with a following comma or the end of the string. Can any Regular Expression Gurus tell me why the first statement doesn't work and the correct pattern to use? Until then I can use the second expression, understanding that the Name/Number value may not actually be enclosed with matching quotes. Thank you.

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      howardjr wrote:

      When I append showDialog='Name' to the url location to my page the following statement returns minus 1, for no match. document.location.search.search( /(^\?|,)showDialog=(['"])?(Name|Number)(\1)?(,|$)/i )

      The \1 backreference is mathing the first capture (^\?|,), not the second capture (['"]). If you want to match the second capture, use \2 instead.

      --- Year happy = new Year(2007);

      H 1 Reply Last reply
      0
      • H howardjr

        When I append showDialog='Name' to the url location to my page the following statement returns minus 1, for no match.

        document.location.search.search( /(^\?|,)showDialog=(['"])?(Name|Number)(\1)?(,|$)/i )
        

        but the next statement returns zero, for a match starting at the begging of the string.

        document.location.search.search( /(^\?|,)showDialog=(['"])?(Name|Number)(['"])?(,|$)/i )
        

        The difference between the two is that the first one is supposed to match the quotes that enclose the word Name or Number. On page 170 of the O'Reilly book, JavaScript The Definitive Guide, by David Flanagan, a very good book on JavaSrcipt, the following example is shown and works:

        /(['"])[^!"]*\1
        

        This pattern looks for either a single or double quote, followed by any other characters, then the matching single or double quote that was initially found. What I need for there to be an optional initial single or double quote, followed by either Name or Number, then the matching quote that matches the initial single or double quote, if any. This pattern is to occur immediately after the url location's query string's question mark or after that after a comma. The pattern should then end with a following comma or the end of the string. Can any Regular Expression Gurus tell me why the first statement doesn't work and the correct pattern to use? Until then I can use the second expression, understanding that the Name/Number value may not actually be enclosed with matching quotes. Thank you.

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #3

        The match at index 0 returns the entire expression, that's why the matches are 1 based, not 0 based.

        Christian Graus - C++ MVP 'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert

        H 1 Reply Last reply
        0
        • G Guffa

          howardjr wrote:

          When I append showDialog='Name' to the url location to my page the following statement returns minus 1, for no match. document.location.search.search( /(^\?|,)showDialog=(['"])?(Name|Number)(\1)?(,|$)/i )

          The \1 backreference is mathing the first capture (^\?|,), not the second capture (['"]). If you want to match the second capture, use \2 instead.

          --- Year happy = new Year(2007);

          H Offline
          H Offline
          howardjr
          wrote on last edited by
          #4

          Thanks!

          1 Reply Last reply
          0
          • C Christian Graus

            The match at index 0 returns the entire expression, that's why the matches are 1 based, not 0 based.

            Christian Graus - C++ MVP 'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert

            H Offline
            H Offline
            howardjr
            wrote on last edited by
            #5

            Ah, thank you for that additional information :)

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups