Skip to content

按规则匹配

可定义多个匹配规则,可进行相应的掩码处理,更加的灵活、可定制化性。

ts
const config: Record<string, [string, string]> = {
  // 两位
  '^[0-9]{2}$': ['^(.{1})(.*)$', '$1{{*$2}}'],
  // 三位以上
  '^[0-9]{3,}$': ['^(.{2})(.*)$', '$1{{*$2}}'],
  // 默认匹配
  '^.*$': ['^(.*)$', '{{*$1}}'],
};

const rulesTestCases = [
  { input: '12', title: '两数字位' },
  { input: '123456', title: '三数字位以上' },
  { input: 'abcdefg', title: '默认匹配' },
];

rulesTestCases.forEach(({ input, title }) => {
  console.log(title, maskText(input, config));
});

// 两数字位: 1*
// 三数字位以上: 12****
// 默认匹配: abcdefg