资讯详情

正则学习笔记

function 入门() { 
          var str = "abc123def";   var reg = /[0-9] /;   var result = str.match(reg);   console.log(result);   // [ '123', index: 3, input: 'abc123def', groups: undefined ] } function 用户创建正式表达式() { 
          /** * 用普通的方法找出所有数字 */   let hd = "houdunren2200hdcms9988";   let nums = [...hd].filter(el => !Number.isNaN(parseInt(el)));   // console.log(nums.join(""))   /** * 用正则表达式找出所有数字 */   // console.log(hd.match(/\d/g).join(""))   /** * 匹配字符串是否有字母U * | 或者运算符 */   let a = "g|@|a";   // console.log(eval(`/${a}/`).test(hd))   let reg = new RegExp(a, "g");   console.log(reg.test(hd)); } function 选择符() { 
          let tel = "010-9999999";   console.log(/^(010|020)\-\d{7,8}$/.test(tel));
}
function 原子表或原子组() { 
       
  let hd = "12";
  let reg = /[1234]/;
  let reg2 = /(12|34)/;
  if (hd.match(reg)) { 
       
    console.log("reg", hd.match(reg).toString());
  }
  if (hd.match(reg2)) { 
       
    console.log("reg2", hd.match(reg2).toString());
  }
}
function 转义() { 
       
  //匹配小数
  let price = "23.34";
  //.除换行外任何字符
  // console.log(/\d+\.\d+/.test(price));
  //在使用方法创建正则表达式时碰到\ 一律写两个,避免\被转义
  let reg = new RegExp("^\\d+\\.\\d+$");
  console.log(reg.test(price));

  let url = "https://www.baidu.com";
  console.log(/https?:\/\/\w+\.\w+\//);
}
function 字符边界约束() { 
       
  let hd = "3dsfds";
  console.log(/^\d/.test(hd));
}
function 数值与空白元字符() { 
       
  let hd = ` 张三:010-9999999,,李四:020-8888888 `;
  console.log(hd.match(/[^-\d:,\s]+/g));
  // []原子表:里面出现的我都要
  // [^]原子表:里面出现的我都不要
  // [ '\n 张三', '李四', '\n ' ]
  // console.log(/\s/.test("hd"))//匹配空白及回车
  // console.log(/^\S/.test(" hd"))//匹配除了空白
}
function w与W元字符() { 
       
  // \w --字母数字下划线
  let hd = "luguohua_@@";
  // console.log(hd.match(/\w+/));
  //匹配邮箱
  let email = "854706462@qq.com";
  // console.log(email.match(/^\w+@\w+\.\w+$/))
  // \W --除了字母数字下划线
  // console.log("hdcms@".match(/\W/))

  let username = "luguohua";
  console.log(/^[a-z]\w{4,9}$/i.test(username));
}
function 点元字符的使用() { 
       
  let url = "https://www.luguohuaniubi.com";
  console.log(url.match(/https?:\/\/www\.\w+\.(com|cn)/g));
  // /xxx/s把内容当做一行展示(把换行符当做空白看)
  let tel = "010 - 99999999";
  console.log(tel.match(/^(010)\s?-\s?\d{8}/));
}
function 匹配所有字符() { 
       
  let xj = "<span>luguohua @@@ hdcms</span>";
  console.log(xj.match(/<span>[\s\S]+<\/span>/));
}
function i与g模式修正符() { 
       
  let hd = "luguohua";
  // i 不区分大小写
  // g 全局匹配
  // 两个一起用直接往后写就行了
  console.log(hd.match(/u/gi));

  // 将所有的u替换成@
  console.log(hd.replace(/u/gi, "@"));
}
function m多行匹配修正符() { 
       
  // m 每一行单独处理
  let hd = ` #1 js,200元 # #2 php,200元 # #3 luguohua.com # 鲁国华 #4 node.js,200元 # `;
  // console.log(hd.match(/\s*#\d{1,}\s{1}\w+,\d+元\s{1}#/))
  var reg = /^\s*#\d{1,}\s+.+\s+#$/gm;
  // console.log(hd.match(reg))
  let lessons = hd.match(reg).map(v => { 
       
    v = v.replace(/\s*#\d+\s*/, "").replace(/\s+#/, "");
    [name, price] = v.split(",");
    return { 
        name, price };
  });
  // console.log(lessons);
  // console.log(JSON.stringify(lessons, null ,2))//用法nice
}
function 汉字与字符属性() { 
       
  // a [L] 匹配字母
  // 在使用L时要配合 模式修正符u
  // 如果有宽字节匹配 也用u来修正模式
  let hd = "luguohua1997.不断打代码,加油";
  // console.log(hd.match(/\p{L}/gu))
  // console.log(hd.match(/\p{P}/gu))
  console.log(hd.match(/\p{sc=Han}/gu));
}
function lastIndex属性的作用() { 
       
  let hd = "luguohua";
  // 原本检索会包含很多属性 用g会丢失
  // console.log(hd.match(/\w/g));
  let reg = /\w/g;
  // lastIndex会一直往下数(只有使用全局模式g的时候才有用)
  // console.log(reg.lastIndex)
  // console.log(reg.exec(hd));
  // console.log(reg.lastIndex)
  // console.log(reg.exec(hd));
  // console.log(reg.lastIndex)
  // console.log(reg.exec(hd));
  while ((res = reg.exec(hd))) { 
       
    console.log(res);
  }
}
function y模式() { 
       
  function 例子() { 
       
    let hd = "udunren";
    let reg = /u/y;
    console.log(reg.exec(hd));
    console.log(reg.lastIndex);
    console.log(reg.exec(hd));
    console.log(reg.lastIndex);
  }
  function 获取QQ群() { 
       
    let hd = `后蹲人QQ群:11111111,99999999,88888888 后蹲人不断分享视频教程,后蹲人网址是 houdunren.com`;
    let reg = /(\d+),?/y;
    reg.lastIndex = 7;
    let qq = [];
    while (res = reg.exec(hd)) qq.push(res[1]);
    console.log(qq);
  }
  // 例子();
  获取QQ群();
}
function 原子表基本使用() { 
       
  //用[]包起来,会把包起来的内容当做一个字符
  let hd = "houdunren";
  // console.log(1,hd.match(/[ue]/g));
  // console.log(2,hd.match(/ue/g));
  let tel = "2022-02-23";
  let reg = /^\d{4}[-\/]\d{2}[-\/]\d{2}$/;
  let reg2 = /^\d{4}([-\/])\d{2}\1\d{2}$/; //原子组
  // \1用第一个原子组 即()里的内容
  console.log(tel.match(reg));
  console.log(tel.match(reg2));
}
function 区间匹配() { 
       
  //只能升序区间
  let hd = "2010";
  console.log(hd.match(/[0-9]+/g));
  let name = "luguohua";
  console.log(name.match(/[a-z]+/g));
}
function 排除匹配() { 
       
  let hd = "houdunren.com";
  // 原子组里加^代表排除[]中匹配的内容
  console.log(hd.match(/[^ue]/gi));
}
function 原子表字符不解析() { 
       s
  let hd = "(houdunren).+";
  // /()/这里面的括号指的是原子组 不是字符括号
  // /[()]/这里的括号指的是括号
  // /[.+]/ 括号里的.+就是字符串自己的意思,没有原子组包裹才有其他含义
  console.log(hd.match(/()/gi));
}
function 使用原子表匹配所有字符() { 
       
  let hd = ` houdunren hdcms `;
  //匹配所有字符
  console.log(hd.match(/.+/gs));
  console.log(hd.match(/[\s\S]+/gs));
}
function 原子组做邮箱验证() { 
       
  let mail = "854706462@qq.com";
  // let reg = /^[\w-]+@[\w-]+\.(com|org|cc|cn|net)$/i;
  let reg = /^[\w-]+@[\w-]+\.(com|org|cc|cn|net)+$/i;
  console.log(mail.match(reg));
}
function 嵌套分组与不记录组() { 
       
  let hd = ` https://www.luguohua.com http://houdunwang.com https://hdcms.com `;
  // ?:不记录原子表 无法用\1引用
  let reg = /https?:\/\/((?:\w+\.)?\w+\.(?:com|cn|org))/gim;
  // console.dir(hd.match(reg))
  let urls = [];
  while (res = reg.exec(hd)) { 
       
    urls.push(res[1]);
  }
  console.log(urls);
}
function 多种重复匹配基本使用() { 
       
  let hd = "hddddddddd";
  // ? 0个到1个
  // {1,3} 1个到3个
  // {1,} 1个到无数个
  console.log(hd.match(/hd{1,3}/));
}
function 重复匹配对原子组影响与电话号正则() { 
       
  // 当用在()后使用+时,+作用的是整个()的内容
  let hd = "hddddhdhdhdhdhdhd";
  console.log(hd.match(/(hd)+/g));
  // 匹配电话号
  let tel = "0478-99999999";
  console.log(tel.match(/^0\d{2,3}-\d{7,8}$/));
}
function 网站用户名验证() { 
       
  /** * 用户名必须以字母开始 * 用户名位数3-8位 * 允许有- */
  let username = "a854706462";
  let reg = /^[a-z]{1}[\w-]{2,7}$/i;
  console.log(reg.test(username));
}
function 批量使用正则完成密码验证() { 
       
  const password = "Aasdasfg";
  const regs = [
    /^[a-z0-9]{5,10}$/i, //字母数字混合密码 五至十位长度
    /[A-Z]/, //必须包含一个大写字母
    /[0-9]/ //必须包含一个数字
  ];
  let state = regs.every(e => e.test(password));
  console.log(state ? "密码正确" : "密码错误");
}
function 禁止贪婪() { 
       
  function 例子() { 
       
    let hd = "hdddd";
    //原本+ * {2,3} 都会往多的进行贪婪操作
    //在后面加上一个? 就会禁止贪婪操作,从最少的开始作用
    console.log(hd.match(/hd+?/)); //1
    console.log(hd.match(/hd*?/)); //0
    console.log(hd.match(/hd{2,3}?/)); //2
    console.log(hd.match(/hd??/)); //0
  }
  function 案例() { 
       
    let main = ` <main> <span>haoren</span> <span>haoren.com</span> <span>dahaoren.com</span> </main> `;
    console.log(main);
    let reg = /<span>([\s\S]+?)<\/span>/gi;
    main = main.replace(reg, (v, p1) => { 
       
      // console.log(v, p1);
      return `<h4 style="color:red">haoren-${ 
         p1}</h4>`;
    });
    console.log(main);
  }
  // 例子();
  案例();
}
function 使用matchAll完成全局匹配() { 
       
  /* -- */
  String.prototype.matchAll = function(reg) { 
       
    let res = this.match(reg);
    if (res) { 
       
      let str = this.replace(res[0], "^".repeat(res[0].length));
      let match = str.matchAll(reg) || [];
      return [res, ...match];
    }
  };
  /* -- */
  let main = ` <main> <h1>haoren</h1> <h2>haoren.com</h2> <h3>dahaoren.com</h3> </main> `;
  let reg = /<(h[1-6])>([\s\S]+?)<\/\1>/gi;
  let hd = main.matchAll(reg); //迭代对象 新浏览器支持matchAll
  // console.log(main.matchAll(reg))
  let contents = [];
  for (const iterator of hd) { 
       
    console.log(iterator);
    contents.push(iterator);
  }
  /
  let hdw = "houdunren";
  console.log(hdw.matchAll(/(u)/i));
}
function 使用exec完成全局匹配() { 
       
  let hd = "houdunren";
  let reg = /u/gi;
  let result = [];
  while (res = reg.exec(hd)) { 
       
    result.push(res) 

标签: hdw306微型拉压传感器

锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

锐单商城 - 一站式电子元器件采购平台