I really need some help figuring out how to make the Regex constructor work. I need to use the constructor b/c I am passing in a variable.
The variable looks like this:
/pages/one.html
or /content/pages/x.html
or /content/pages/x.html?cachebust=1&refresh=yesplease&one=1
Works
var regLit = /\/pages\/x\.html(\?success=true)?/g
Fails
var regCon = new RegExp("\/pages\/x\.html(\?success=true)?", "g")
Here is my code. Please help SO!!!
var closeMatch = function (list1, list2) {
if (list1.length === list2.length) {
return list1.every(function(item, index) {
let re = item
.replace(/[\/]/g, '\\/')
.replace(/\./g, '\\.')
.concat('(\?success=true)?');
console.log(re, list2[index]);
return new RegExp(re, 'g').test(list2[index]);
});
} else {
return false;
}
};
var arr1 = ['/pages/one.html', '/content/pages/x.html','/content/pages/x.html?success=true'];
var arr = ['/pages/one.html', '/content/pages/x.html','/content/pages/x.html?success=true&funnel=done&someother=blah'];
console.log(closeMatch(arr1, arr));