js:求一个正则?
const tableList = [
"account.t_account",
"account.t_account_ext",
"account.t_sub_account",
"account.account.t_sub_account",
"t_sub_accountok"
]
const databaseList = [
"account"
]
js:求一个正则,例如上述例子, tableList 处理之后,可以得到
[
"t_account",
"t_account_ext",
"t_sub_account",
"t_sub_account",
"t_sub_accountok"
]
回复
1个回答

test
2024-07-01
感觉不需要借助正则啊,直接用 filter
加上 split
就可以满足需求了呀?
const resultList = tableList
.filter(value => databaseList.some(key => value.includes(key))) // 过滤匹配项目
.map(value => value.split('.').pop()) // 收集最后一项属性
// Array(5) [ "t_account", "t_account_ext", "t_sub_account", "t_sub_account", "t_sub_accountok" ]
回复

适合作为回答的
- 经过验证的有效解决办法
- 自己的经验指引,对解决问题有帮助
- 遵循 Markdown 语法排版,代码语义正确
不该作为回答的
- 询问内容细节或回复楼层
- 与题目无关的内容
- “赞”“顶”“同问”“看手册”“解决了没”等毫无意义的内容