Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Javascript (http://www.velocityreviews.com/forums/f68-javascript.html)
-   -   match(regexp) deciphering (http://www.velocityreviews.com/forums/t923268-match-regexp-deciphering.html)

brad 02-28-2006 04:14 AM

match(regexp) deciphering
 
Hello all, I'm new to javascript--not too new to a few other
programming languages--and I need your help deciphering the Regexp in
the following string. Regular expresions are hard enough in Python, and
since I am new to javascript they are even harder. Well here's the
string, thanks for any and all help I receive.


document.URL.match(/^(.+?)(?:\?(?:(.*?)@)?(.+))?$/)


Thomas 'PointedEars' Lahn 02-28-2006 06:33 AM

Re: match(regexp) deciphering
 
brad wrote:

> Hello all, I'm new to javascript--not too new to a few other
> programming languages--and I need your help deciphering the Regexp in
> the following string. Regular expresions are hard enough in Python, and
> since I am new to javascript they are even harder. Well here's the
> string, thanks for any and all help I receive.
>
> document.URL.match(/^(.+?)(?:\?(?:(.*?)@)?(.+))?$/)


Match
the beginning of input (`^')
followed by
one or more occurrences (`+') of any character except newline (`.'),
shortest match wins (`?')
followed by none or one occurrence (`?') of
the literal character `?' (`\?')
followed by zero or one occurrence (`?') of
zero or more occurrences (`*') of any character except newline (`.'),
shortest match wins (`?')
followed by
`@'
where the match of the subexpression is not captured (`(?:...)'),
where the match of the subexpression is not captured (`(?:...)'),
followed by
one or more occurrences of any character except newline
followed by
the end of input
in the value of `document.URL' and return a reference to an Array-like
object (one including numerically iterable properties) with each match
of a captured paranthesed subexpression being available as property of
that object with numerical name, starting with "0" as the property name
for the first captured match.

<URL:http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:RegEx p>
<URL:http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Strin g:match>

(Once you have learned how to read and interpret Regular Expressions,
they are no mystery to you anymore, no matter the used flavour. See
also <URL:http://oreilly.com/catalog/regex/>; the sample chapters
sufficed for me to date.)

Note that non-capturing parantheses are not backwards compatible. It is
better not to use them here, and to use the backreference with greater
index instead.

The code does not make sense because it appears to check for some form of
URI-inline authentication while it also indicates it runs in a client-side,
HTML environment (`URL' is a property of HTMLDocument DOM host objects).
However, this code must not apply to a valid http(s) URI; usernames and
passwords are not allowed in them and user agents that support them there
anyway are broken. Meaning that this is a security leak there that will be
fixed soon or is already fixed in the next version. So this is an approach
that is not at all reliable.


PointedEars

Thomas 'PointedEars' Lahn 02-28-2006 03:16 PM

Re: match(regexp) deciphering
 
Thomas 'PointedEars' Lahn wrote:

> brad wrote:
>> Hello all, I'm new to javascript--not too new to a few other
>> programming languages--and I need your help deciphering the Regexp in
>> the following string. Regular expresions are hard enough in Python, and
>> since I am new to javascript they are even harder. Well here's the
>> string, thanks for any and all help I receive.
>>
>> document.URL.match(/^(.+?)(?:\?(?:(.*?)@)?(.+))?$/)

>
> [...] and return a reference to an Array-like object (one including
> numerically iterable properties) with each match of a captured paranthesed
> subexpression being available as property of that object with numerical
> name, starting with "0" as the property name for the first captured match.


Sorry, that is nonsense. Should have been

.... starting with "0" as the property name for the match of the whole
expression, "1" for the captured match of the first paranthesed
subexpression (here: /.+?/) and so on.


PointedEars


All times are GMT. The time now is 01:49 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.