const columnDateIndex = [
{ path: "2023|Jan 2023" },
{ path: "2023|Feb 2023" },
{ path: "2023|Mar 2023" },
{ path: "2023|Apr 2023" },
{ path: "2023|May 2023" },
{ path: "2023|Jun 2023" },
{ path: "2023|Jul 2023" },
{ path: "2023|Aug 2023" },
{ path: "2023|Sep 2023" },
{ path: "2023|Oct 2023" },
{ path: "2023|Nov 2023" },
{ path: "2023|Dec 2023" },
{ path: "2024|Jan 2024" },
{ path: "2024|Feb 2024" },
{ path: "2024|Mar 2024" },
{ path: "2024|Apr 2024" },
{ path: "2024|May 2024" },
{ path: "2024|Jun 2024" },
{ path: "2024|Jul 2024" },
{ path: "2024|Aug 2024" },
{ path: "2024|Sep 2024" },
{ path: "2024|Oct 2024" },
{ path: "2024|Nov 2024" },
{ path: "2024|Dec 2024" },
];
const columnPath = "Williams|Bonus|2024|Feb 2024";
const result = columnDateIndex.find((column) => {
const re = new RegExp(`${column.path}$`);
return re.test(columnPath);
});
console.log(result);
输出: { 路径: '2024|2024 年 1 月' }
预期输出: {路径:“2024年|2024年2月”}
我想获得准确的最终匹配。
endsWith
:
const columnDateIndex = [
{ path: "2023|Jan 2023" },
{ path: "2023|Feb 2023" },
{ path: "2023|Mar 2023" },
{ path: "2023|Apr 2023" },
{ path: "2023|May 2023" },
{ path: "2023|Jun 2023" },
{ path: "2023|Jul 2023" },
{ path: "2023|Aug 2023" },
{ path: "2023|Sep 2023" },
{ path: "2023|Oct 2023" },
{ path: "2023|Nov 2023" },
{ path: "2023|Dec 2023" },
{ path: "2024|Jan 2024" },
{ path: "2024|Feb 2024" },
{ path: "2024|Mar 2024" },
{ path: "2024|Apr 2024" },
{ path: "2024|May 2024" },
{ path: "2024|Jun 2024" },
{ path: "2024|Jul 2024" },
{ path: "2024|Aug 2024" },
{ path: "2024|Sep 2024" },
{ path: "2024|Oct 2024" },
{ path: "2024|Nov 2024" },
{ path: "2024|Dec 2024" },
];
const columnPath = "Williams|Bonus|2024|Feb 2024";
const result = columnDateIndex.find((column) => {
return columnPath.endsWith(column.path);
});
console.log(result);
你必须逃离|和空格字符,它们应该被视为文字字符
const columnDateIndex = [
{ path: "2023|Jan 2023" },
{ path: "2023|Feb 2023" },
{ path: "2023|Mar 2023" },
{ path: "2023|Apr 2023" },
{ path: "2023|May 2023" },
{ path: "2023|Jun 2023" },
{ path: "2023|Jul 2023" },
{ path: "2023|Aug 2023" },
{ path: "2023|Sep 2023" },
{ path: "2023|Oct 2023" },
{ path: "2023|Nov 2023" },
{ path: "2023|Dec 2023" },
{ path: "2024|Jan 2024" },
{ path: "2024|Feb 2024" },
{ path: "2024|Mar 2024" },
{ path: "2024|Apr 2024" },
{ path: "2024|May 2024" },
{ path: "2024|Jun 2024" },
{ path: "2024|Jul 2024" },
{ path: "2024|Aug 2024" },
{ path: "2024|Sep 2024" },
{ path: "2024|Oct 2024" },
{ path: "2024|Nov 2024" },
{ path: "2024|Dec 2024" },
];
const columnPath = "Williams|Bonus|2024|Feb 2024";
const result = columnDateIndex.find((column) => {
const pattern = column.path.replace('|', '\\|').replace(' ', '\\s');
const re = new RegExp(`${pattern}$`);
return re.test(columnPath);
});
console.log(result);