項目中一些用到的正則匹配場景

  trim(str) {
    // 刪除左右兩端的空格
    return str.replace(/(^\s*)|(\s*$)/g, "");
  }
  // 限制文本框只能輸入正數、小數、負數
  limitConst(str) {
    return str.replace(/[^\-?\d.]/g, "");
  }
  // 去除中文
  removeCinString(str) {
    let reg = /[^a-zA-Z]/g;
    return this.removeEinString(str.replace(reg, ""));
  }
  // 去除英文
  removeEinString(str) {
    let reg = /[u4E00-u9FA5]/g;
    return str.replace(reg, "");
  }
  // 判斷是否中英文
  verifyCe(str) {
    let reg = /[^[\u4e00-\u9fa5_a-zA-Z]+$]/g;
    return reg.test(str);
  }
  // 匹配 +-×÷()/*
  limitSign(str) {
    let arr = str.split("");
    let newIndex, newStr;
    arr.forEach((text, index) => {
      if (this._singString.indexOf(text) != -1) newIndex = index;
    });
    if (arr[newIndex]) {
      newStr = arr[newIndex];
    } else {
      newStr = "";
    }
    return newStr;
  }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章