小程序中手机号识别

小程序中手机号识别

后端返回字符串包好手机号,点击可拨打电话

  • split技巧
  • 注意的点是这个正则regPhone,里面加了有括号,如果separator是包含捕获括号的正则表达式(),则匹配结果包含在数组中
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    let _str = "xxx,联系电话:18839687266。稍后与您联系。xxx。";
    parseStr (str) {
    const regPhone = /(1\d{10})/;
    const list = str.split(regPhone);
    const result = [];
    return list.map(item => {
    let tag;
    regPhone.test(item) ? tag = 'phone' : tag = 'text';
    return {
    type: tag,
    text: item
    }
    })
    }