A question about two regular expression patterns, please help.
-
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.
-
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.
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);
-
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.
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
-
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);
-
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