www.webdeveloper.com
Results 1 to 4 of 4

Thread: Regular expression for URL

  1. #1
    Join Date
    Dec 2012
    Posts
    2

    Regular expression for URL

    Hello,

    Please, help me to fix this regular expression to make it workable in JavaScript:
    Code:
    var r = /^((((ht|f)tp(s?))\://)|(/{1}))?((([a-zA-Z0-9_\-]{2,}\.)+[a-zA-Z]{2,})|((?:(?:25[0-5]|2[0-4]\d|[01]\d\d|\d?\d)(?(\.?\d)\.)){4}))(:[a-zA-Z0-9]+)?(/[a-zA-Z0-9\-\._\?\,\'/\\\+&%\$#\=~]*)?$/;
    In fact, here it works fine, but only if i choose .NET Regex Engine. When i'm using JavaScript one, it doesn't even run.

    What have i tried is to screen this place "...//..." with "...\/\/...", but it doesn't help.

    Thanks!

  2. #2
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,271
    You have to escape no only the two first slashes but the followings...
    It would be good to know what it has to make (http or ftp with a possible s but after (\/{1}) ???) rather than try to guess it !

  3. #3
    Join Date
    Dec 2012
    Posts
    2
    Actually, i had next regexp, but extended it by myself for understanding relative paths like "/folder/other":
    Code:
    ^(((ht|f)tp(s?))\://)?((([a-zA-Z0-9_\-]{2,}\.)+[a-zA-Z]{2,})|((?:(?:25[0-5]|2[0-4]\d|[01]\d\d|\d?\d)(?(\.?\d)\.)){4}))(:[a-zA-Z0-9]+)?(/[a-zA-Z0-9\-\._\?\,\'/\\\+&%\$#\=~]*)?$

  4. #4
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,271
    I do not understand this regular expression.

    I see four parts (I add \ before the javascript delimiter / for regular expressions) :

    1/ ^(((ht|f)tp(s?))\:\/\/)?
    2/ ((([a-zA-Z0-9_\-]{2,}\.)+[a-zA-Z]{2,})|((?: (?:25[0-5]|2[0-4]\d|[01]\d\d|\d?\d)(?(\.?\d)\.)){4})) with a green alternative
    3/ (:[a-zA-Z0-9]+)?
    4/ (/[a-zA-Z0-9\-\._\?\,\'\/\\\+&%\$#\=~]*)?$

    The first one is optional ( )?
    The second is made with
    • at least two alphanumeric characters, followed by a point and at least two alphabetic characters
    • or :
      • 25 followed by a number included between 1 and 5,
      • 2 followed by a number included between 0 and 4 and a digit,
      • 0 or 1 followed by two digits,
      • one or two digits,
      followed by something like (?(\.?\d)\.) which make not sense the parenthesis cannot be optional. (?: ) would be a non-capturing sub-patterns ?
      //...
      The fourth part is also curious with a amp in a class which contains all alphabetic characters...

    If you want to use an alternative with /folder or /other you have to specify the part to replace ...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles