2

I'm trying to check for match in an array with PURE JAVASCRIPT. I don't know how to do this, I would appreciate your help.

var sites = new Array ("site1.com", "site2.com", "site3.com" ...);
// Sites array contains 100 values

var imgs = document.getElementsByTagName("img");
    for (var i = 0; i < imgs.length; i++) {
        img = imgs[i].src;

        // I'm trying to check if is in array, 
        // and don't waste a lot of size in code

        if(img.match(sites)){
            notHere(imgs[i]);
        }

        // This is the working way.
        // Even if location is a.site1.com/b/, it will match 
        if (img.match("site1.com")) {
            heReload(imgs[i]);
        }
        // Repeat this piece of code 100 times
    }
}

NOTE: I don't want to check for an exact value. I want to simulate the match() function so if img = "http://a.b.c/d/" and in array is "b.c/", it executes function().

3 Answers 3

1

Your "sites" variable should be a regular expression rather than an array:

 var sites = /\b(site1\.com|site2\.com|etc|etc)\b/

later:

 if (img.match(sites))
    ......

If for some reason you prefer to keep "sites" in an array, you also can create a regular expression "on the fly":

var sites = ["site1.com", "site2.com", "site3.com"]
var sitesRegexp = new RegExp("\\b(" + sites.join("|").replace(".", "\\.") + ")\\b")

 ....

 if (img.match(sitesRegexp)
    ......
Sign up to request clarification or add additional context in comments.

1 Comment

Do not forget to escape special RegEx characters! A dot is not the only special character. .replace(/[[^$.|?*+(){}\\]/g, '\\$&');
1

Good use case for filter.

If you want to have it working on "old" browser :

var nativeFilter = Array.prototype.filter;
_.filter = _.select = function(obj, iterator, context) {
    var results = [];
    if (obj == null) return results;
    if (nativeFilter && obj.filter === nativeFilter) return obj.filter(iterator, context);
    each(obj, function(value, index, list) {
      if (iterator.call(context, value, index, list)) results[results.length] = value;
    });
    return results;
};

It will return an empty array if nothing is found, otherwise, return an array containing the result(s)

> [1,2,3,4].filter(function(item) { return item == 4 } );
[ 4 ]

Source : Underscore.js

So now your code will look like this :

var sites = new Array ("site1.com", "site2.com", "site3.com" ...);
// Sites array contains 100 values

var imgs = document.getElementsByTagName( "img" );
for ( var i = 0; i < imgs.length; i++ ) {
    var img = imgs[ i ].src;
    var result = sites._filter( function( site ) {
        return img.match( site )
    });

    // result is an array of matching sites

}

Comments

0

you can extend the Array prototype , so it supports all browsers ...

try this :

Array.prototype.myContains = function(obj) {
    var i = this.length;
    while (i--) {if (this[i] .indexOf(obj)>-1)  return true; }
    return false;
}

usage : var t=myArr.myContains(3);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.