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:


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 :slight_smile:

Do you only need to match that one URL?

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



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.

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:


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:


var ...,
    i,
    urlsLen = urls.length;
for (i = 0; i < urlsLen; i += 1) {
    ...
}

var …,
i,
urlsLen = urls.length;
for (i = 0; i < urlsLen; i += 1) {

}

Good to know :slight_smile:
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.

AFAIK, no there isn’t a difference. Take this test example :slight_smile:

<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++;

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.