SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict
    Join Date
    Sep 2004
    Location
    Denver
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    regular expression for url

    Greetings, can someone help me fix this reg exp for url validation or if you use a better one than this.
    I need to have this allow urls without an extention. (ie: www.mysite.com/contact)

    /^(http:\/\/www.|https:\/\/www.|ftp:\/\/www.|www.){1}([\w]+)(.[\w]+){1,2}$/

    Thanks.

  2. #2
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    /^(https?|ftp)\:\/\/www(\.\w+)+(\/|\/\w+)+(\.\w+)?$/i

    Don't forget to escape dots! (\.)
    In a regexp a period matches anything but a newline.
    And you don't need to bracket a single token- [\w] =\w

  3. #3
    SitePoint Addict
    Join Date
    Sep 2004
    Location
    Denver
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    mrhoo, the regular expression you're suggesting doesn't accept urls like this
    http://www.mysite.com. Any thoughts from anyone on helping me with a good regular expression for url validation is appreciated.

  4. #4
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your example implied you wanted a subfolder. To accept www.mysite.com
    change the fourth'+' to '?'.

    var rX=/^(https?|ftp)\:\/\/www(\.\w+)+(\/|\/\w+)?(\.\w+)?$/i

  5. #5
    SitePoint Addict
    Join Date
    Sep 2004
    Location
    Denver
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    mrhoo, this is looking better now. thank you.
    How do I go about making the (www) optional so it accepts http://mysite.com

  6. #6
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    var re=/^(https?|ftp):\/\/[\w-]+(\.[\w-]+)+(\/[\w-]*)*$/
    var test = [
    	"http://example.com",
    	"http://www.example.com",
    	"http://example.com/subdir",
    	"http://sub-domain.example.com/subdir1/subdir2",
    	"foobar",
    	"http://example.com/subdir/index.php"
    ];
    for(var i = 0; i < test.length; i++)
    	test[i] = i + (test[i].match(re) ? " pass " : " FAIL ") + test[i]
    alert(test.join("\n"))

  7. #7
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    chiti, it will pay you to read up on regular expressions.

    to make a pattern optional, enclose it in parentheses and follow it
    with ? (for none or 1 instance) or * (for none or any number of repeitions).
    a plus sign (+) means at least one, plus any number of repetitions,
    and if you want at least 2 but no more than 3 repetitions,
    follow the parenthesized expression with{2,3}
    Last edited by mrhoo; Dec 30, 2006 at 12:36.


Bookmarks

Posting Permissions

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