SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Regex check a domain in a url

    Hi,
    I have to parse an array of urls and get
    only the url with a target domain.
    I ended up with:
    Code JavaScript:
    var regEx = /www.emp-online.it/;
    var urls = [
        'http://www.emp-online.it/bin/shop.php?prog=shop&funktion=PRODUCTINFO&article=670017',
        'http://www.masteringapi.com/tutorials/how-to-check',
        'http://facebook.stackoverflow.com/questions/3548493/how-to-',
        'http://www.emp-online.it/art_670017/'
    ];
    for(var i = 0,len = urls.length; i < len; i++){
        if(regEx.test(urls[i])){
            console.log(urls[i]);
        }
    }

    Is it the right way ?
    Is there a better way ?

    When I have to deal with regex
    I'm always doubtful

  2. #2
    SitePoint Guru Jason__C's Avatar
    Join Date
    Oct 2009
    Location
    Racoon City
    Posts
    660
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by whisher View Post
    Hi,
    I have to parse an array of urls and get
    only the url with a target domain.
    I ended up with:
    Code JavaScript:
    var regEx = /www.emp-online.it/;
    var urls = [
        'http://www.emp-online.it/bin/shop.php?prog=shop&funktion=PRODUCTINFO&article=670017',
        'http://www.masteringapi.com/tutorials/how-to-check',
        'http://facebook.stackoverflow.com/questions/3548493/how-to-',
        'http://www.emp-online.it/art_670017/'
    ];
    for(var i = 0,len = urls.length; i < len; i++){
        if(regEx.test(urls[i])){
            console.log(urls[i]);
        }
    }

    Is it the right way ?
    Is there a better way ?

    When I have to deal with regex
    I'm always doubtful
    Do you only need to match that one URL?

    If you want to validate all use an expression like this:


    Code JavaScript:
     
    var regEx = new RegExp("((http|https)(:\/\/))?([a-zA-Z0-9]+[.]{1}){2}[a-zA-z0-9]+(\/{1}[a-zA-Z0-9]+)*\/?", "i");

    Not sure you need a RegExp for this. Paul will probably chime in.
    Chuck Norris is so tough,
    mosquitos ask for permission before they bite him

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by USPaperchaser View Post
    Not sure you need a RegExp for this. Paul will probably chime in.
    You're right.

    If he's just looking to see if the domain 'www.emp-online.it' is in there, it can be achieved more simply with:

    Code javascript:
    var domain = 'www.emp-online.it',
        urls = [
        'http://www.emp-online.it/bin/shop.php?prog=shop&funktion=PRODUCTINFO&article=670017',
        'http://www.masteringapi.com/tutorials/how-to-check',
        'http://facebook.stackoverflow.com/questions/3548493/how-to-',
        'http://www.emp-online.it/art_670017/'
    ],
        i;
    for (i = 0; i < urls.length; i += 1) {
        if (urls[i]).search(domain) > -1) {
            console.log(urls[i]);
        }
    }

    As a separate issue, declaring variables within the for loop does not restrict them to there. JavaScript does not have block scope. There is only function scope that currently applies to JavaScript. This means that wherever a variable is declared, that declaration is automatically hoisted to the top of the function.

    Because it can be confusing for what you have written to differ from what actually occurs, it's a best-practice to declare all of the variables from one var statement right at the start of the function that they are in. In this case you have no functions, so it's just right at the start instead.

    Caching the loop length also does not provide any significant improvement, but if you were to do it then it could be done in the following manner:

    Code javascript:
    var ...,
        i,
        urlsLen = urls.length;
    for (i = 0; i < urlsLen; i += 1) {
        ...
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #4
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    var ...,
    i,
    urlsLen = urls.length;
    for (i = 0; i < urlsLen; i += 1) {
    ...
    }
    Good to know
    Thanks buddies.
    Btw is there any difference doing this
    or (i = 0; i < urlsLen; i += 1)
    instead of
    or (i = 0; i < urlsLen; i ++)

    Thanks in advance.

  5. #5
    billycundiff{float:left;} silver trophybronze trophy RyanReese's Avatar
    Join Date
    Oct 2008
    Location
    Whiteford, Maryland, United States
    Posts
    13,564
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)
    AFAIK, no there isn't a difference. Take this test example
    Code:
    <script type="text/javascript">var x;
    x=5;
    document.write(x);
    document.write("<br />");
    
    
    x= x+=1;
    document.write(x);
    document.write("<br />");
    </script>
    It echos the same result as if you did x++;
    Twitter-@Ryan_Reese09
    http://www.ryanreese.us -Always looking for web design/development work

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by whisher View Post
    Btw is there any difference doing this
    or (i = 0; i < urlsLen; i += 1)
    instead of
    or (i = 0; i < urlsLen; i ++)
    Not directly, but it has benefits later on.

    It means that if you need a loop to increase by 2, or by some other value (returned from a function perhaps) then the i += 1 version is easier and more consistent to modify.

    The other reason that I have for staying away from ++ is that it doesn't improve, the code or make it faster, or easier to understand, or to modify. So, since ++ provides no realistic benefit - I consider it to to not be a useful part of JavaScript.

    It's not directly harmful, but it does have the potential to harm when used in other places instead of clearer techniques.
    So my personal standard is to not use ++, which also prevents me from being tempted use it in other parts of the code in potentially confusing ways.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •