微信小程序自动识别姓名电话地址

 

举例数据:

var str = "姓名:悬剑电话:18686868866地址:北京市五道口职业技术学院";

var str2 = "悬剑18686868866北京市五道口职业技术学院";

 

复制代码
  changtext: function(e) {
    var that = this,
      text = e.detail.value,
      text = text.replace(/(^\s*)|(\s*$)/g, "");
    console.log(text);

    if (text == '') {
      that.setData({
        u_name: '',
        u_phone: '',
        u_address: ''
      })
      return;
    }

    var regx = /(1[3|4|5|7|8][\d]{9}|0[\d]{2,3}-[\d]{7,8}|400[-]?[\d]{3}[-]?[\d]{4})/g;
    var phone_num = text.match(regx);
    if (phone_num != null) {
      var phone = text.indexOf(phone_num[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
      })
    } else {
      that.setData({
        u_name: '',
        u_phone: '',
        u_address: ''
      })
      return;
    }
复制代码

输出:

var u_name = "悬剑",
    u_phone = "18686868866",
    u_address = "北京市五道口职业技术学院";
posted @   悬剑丶  阅读(3439)  评论(3编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示