我真的需要一些帮助来弄清楚如何使Regex构造函数工作。我需要使用构造函数b / c我传入一个变量。
变量如下所示:
/pages/one.html
或/content/pages/x.html
或/content/pages/x.html?cachebust=1&refresh=yesplease&one=1
作品
var regLit = /\/pages\/x\.html(\?success=true)?/g
失败
var regCon = new RegExp("\/pages\/x\.html(\?success=true)?", "g")
这是我的代码。请帮帮忙!!!
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));