#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    150
    Rep Power
    3

    Question Detect various web site link formats in textarea - no strip, no replace


    Hi all,

    I'd like to detect, but not remove, not replace/strip, various formats for web site url's. Such as:

    http://www.a.com
    http://a.com
    http:// www . a . com
    http : // www a dot com
    www.a.com
    www . a . com
    www dot a dot com

    I've tried these, but they don't work:

    Code:
        var strFindWebSites=/^\(?[http]\)?[\. ]?[www][\. ]?[com]$/g;
        if (strFindWebSites.test(theForm.theTextArea.value)) {
    ...
    Code:
        var strFindWebSites=/^\(?([http])\)?[\. ]?([www])[\. ]?([com])$/g;
        if (strFindWebSites.test(theForm.theTextArea.value)) {
    ...
    Code:
        var strFindWebSites=/^\(?(http)\)?[\. ]?(www)[\. ]?(com)$/g;
        if (strFindWebSites.test(theForm.theTextArea.value)) {
    ...
    I realize that there are so many ways that people could enter a web site address, and I'm not trying to capture every possible way. Simply trying to capture the more common ways, and those that would include a space in them.

    Ideas?

    Thanks.
  2. #2
  3. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,134
    Rep Power
    120
    It looks like your wanting domain forwards or domain error handling; but this is not JavaScript... that does this. You could try to create a "catch all" in you htaccess file; to redirect the urls to your home page or an error page or where ever you want them to go.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    150
    Rep Power
    3
    Thanks for the reply.

    I'm not looking for domain forwards or domain error handling.

    Just wanting to detect if anyone has inputed any textarea information that contains a web site address, as above.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    150
    Rep Power
    3
    Update:

    I've gotten this to work, just not sure this is the "best" way to approach this:

    Code:
    var strFindWebSites=["www", "w w w", "http", "h t t p", "href", "h r e f", ".com", "dot com", ".net", "dot net"];
        var searchExp = new RegExp(strFindWebSites.join("|"),"gim");
    
        if (searchExp.test(theForm.theTextArea.value)) {
    
    ...
    I also realize that there is a chance that someone might type in something other than a web address with "polka dot netting" <-- That would be caught by "dot net"

    How could I find the whole word string "dot net" only (in this case)?
    Last edited by we5inelgr; February 17th, 2013 at 01:36 AM.
  8. #5
  9. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,134
    Rep Power
    120
    Originally Posted by we5inelgr
    I also realize that there is a chance that someone might type in something other than a web address with "polka dot netting" <-- That would be caught by "dot net"

    How could I find the whole word string "dot net" only (in this case)?

    You could check the string length; if it is below a specific number... use regex. Otherwise, skip over the text in the string/html or whatever.

    Just wondering... what are you going for, with this? Are you trying to strip out spam domains, for an end user post or are you doing this for advertising or something else?
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    150
    Rep Power
    3
    Ah yes. Great idea.

    This is just to flag entries for potential follow-up.

    Thanks.

IMN logo majestic logo threadwatch logo seochat tools logo