手机号@符号的处理

在工作中因为区号和手机号没区分处理为了拆分中间加了个@符号,当需要展示的时候需要把@符号去掉,这里用的是一个过滤器:

phoneFormat(input) {
      if (input) {
        if (input.indexOf("@") != -1) {
          return input.replace("@", "-");
        } else {
          return input;
        }
      } else {
        return "-";
      }
    }

indexOf()方法返回调用它的字符串对象中第一次出现指定值的索引,如果未找到该值,则返回-1
replace()方法返回一个由替换值替换一些或所有匹配后的新字符串
或者是在获得数据的时候就做处理:

if (cargoDetail.user_phone.indexOf("@") != -1) {
  let tel = cargoDetail.user_phone.split("@");
  cargoDetail.user_phone = tel[0] + "-" + tel[1];
}

split()方法使用指定的分隔符字符串将一个String对象分割成子字符串数组,以一个指定的分割字串来决定每个拆分的位置

原文地址:https://www.cnblogs.com/my466879168/p/12382682.html