Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / HTML, CSS, Scripts / JavaScript / February 2006



Tip: Looking for answers? Try searching our database.

match(regexp) deciphering

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
brad - 28 Feb 2006 04:14 GMT
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 - 28 Feb 2006 06:33 GMT
> 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
[quoted text clipped - 3 lines]
>
> 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_Object
s:RegExp
>
<URL:http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Object
s:String: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 - 28 Feb 2006 15:16 GMT
>> 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
[quoted text clipped - 8 lines]
> 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
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.