一 要实现效果如下:
二 主要代码如下:
const text = '文案一18890909090文案二400-8388388文案三010-1234567' let textArray = text.split(/(\d{3,4}-\d{7,8}|1\d{10})/) let textRes = [] for (const key in textArray) { const flag = textArray[key] != '' && /^(\d{3,4}-\d{7,8}|1\d{10})$/.test(textArray[key]) textArray[key] != '' && textRes.push({ type: flag ? 'phone' : 'text', val: textArray[key] }) } console.log(textRes)