微信小程序自动识别姓名电话地址
作者:互联网
举例数据:
var str = "姓名:悬剑电话:18686868866地址:陕西省西安市雁塔区大雁塔街道广场"; var str2 = "悬剑18686868866陕西省西安市雁塔区大雁塔街道广场";
changtext: function(e) { var that = this, text = e.detail.value, text = text.replace(/(^\s*)|(\s*$)/g, ""); var regx = /(1[3|4|5|7|8][\d]{9}|0[\d]{2,3}-[\d]{7,8}|400[-]?[\d]{3}[-]?[\d]{4})/g; var phoneNums = text.match(regx); if (phoneNums.length != null) { var phone = text.indexOf(phoneNums[0]); } var name = text.indexOf("姓名:"); if (name >= 0) { var phone = text.indexOf("电话:"), address = text.indexOf("地址:"); var u_name = text.substring(name + 3, phone), u_phone = text.substring(phone + 3, address), u_address = text.substring(address + 3, text.length); that.setData({ u_name: u_name, u_phone: u_phone, u_address: u_address }) } else if (phone >= 0) { var u_name = text.substring(0, phone), u_phone = text.substring(phone, phone + 11), u_address = text.substring(phone + 11, text.length); that.setData({ u_name: u_name, u_phone: u_phone, u_address: u_address }) } },
标签:自动识别,name,微信,address,substring,phone,地址,text,var 来源: https://www.cnblogs.com/xuanjiange/p/12119713.html