I have a very strage problem:
var cat = [ { slug: "test/foo", id: 1}, { slug: "test/bar", id: 1}];
var searchreg = new RegExp("test","g");
cat.forEach(function(item){
if(searchreg.test(item.slug)){
console.log(item);
}
});
This should print me both items out of "cat". But I only get the first. If I add more items I only get every 2nd (start with the first).
I get it woking with:
var cat = [ { slug: "test/foo", id: 1}, { slug: "test/bar", id: 1}];
cat.forEach(function(item){
var searchreg = new RegExp("test","g");
if(searchreg.test(item.slug)){
console.log(item);
}
});
But I don't understand why this don't work (Chrome) - anyone a Idea?
var searchreg = /test/g;
(or of course, per my answer, leave out theg
). It's also worth mentioning you don't actually need regex for that,if (item.slug.indexOf("test") !== -1)
would also work.