antd的table的cloumn的render函数过滤字符串

205 阅读1分钟
const week: string[] = ['周一', '周二', '周三', '周四', '周五'];
const tableCloumns = [
     {
        title: '上课时间',
        dataIndex: 'classTime',
        render: (text: string) => '' // text = '1,2,3,4,5'
      }
]

错误方法

  • RegExp['$&'] 这个东西代表的是匹配的最后一个字符。(害我浪费了俩小时)
render: (text: string) => text.replace(/\d/g, (match: string) => week[Number(RegExp['$&']) - 1])
  • 我想要的是当前匹配的字符

正解

  • 浪费两个小时是因为我觉得用RegExp['xx']方法很酷,一直在找RegExp匹配当前字符的方法
  • 吸取教训: 写代码勿搞花里胡哨
render: (text: string) => text.replace(/\d/g, (match: string) => week[Number(match) - 1])