1、过滤数字字母以…结尾

  1. <script>
  2. var str = "https://www.baidu.com/12455";
  3. var reg = /[0-9]/g;
  4. console.log(str.replace(reg,""));
  5. var arr = [
  6. {id:1001,name:"你好3455ffd"},
  7. {id:1001,name:"互戳撒网我3455ffd"},
  8. {id:1001,name:"后台服务3455ffd"}
  9. ]
  10. var reg = /[0-9a-z]/g;
  11. arr.forEach(item=>{
  12. var name = item.name.replace(reg,"")+"...";
  13. item.name = name;
  14. })
  15. console.log(arr);
  16. </script>

2、输入手机号

  1. <body>
  2. <input type="text" id="app">
  3. <script>
  4. // ^(086)?1[356789]\d{9}$
  5. var reg = /^(086)?1[3-9]\d{9}$/;
  6. $("#app").keyup(function(event){
  7. if(event.keyCode == 13){
  8. var value = $(this).val();
  9. console.log(reg.test(value));
  10. }
  11. })
  12. </script>
  13. </body>

3、var reg = /(http|https):\/\/www.baidu.com/;

  1. <script>
  2. var url = "http://www.baidu.com";
  3. var https = "http://www.baidu.com";
  4. var reg = /(http|https):\/\/www\.baidu\.com/;
  5. console.log(reg.test(url))
  6. console.log(reg.test(https))
  7. </script>

4、身份证

  1. <script>
  2. //42...x
  3. // 42\d{15} (\d|x)
  4. // 42\d{15}[\dx]
  5. var str = "420528199108282529"
  6. var reg = /^42\d{15}[\dx]$/;
  7. console.log(reg.test(str));
  8. </script>

5、密码

  1. <input type="text" id="app">
  2. <script>
  3. //密码 必须包含大写小写至少六位数字
  4. var reg = /^[A-Z]+[a-z]\d{6,}$/ //开头必须是大写字母
  5. $("#app").keyup(function (event) {
  6. if (event.keyCode == 13) {
  7. var value = $(this).val();
  8. console.log(value);
  9. console.log(reg.test(value));
  10. }
  11. })
  12. </script>

6、豆瓣 霸王别姬

  1. <p class="">
  2. 导演: 陈凯歌 Kaige Chen&nbsp;&nbsp;&nbsp;主演: 张国荣 Leslie Cheung / 张丰毅 Fengyi Zha...<br>
  3. 1993&nbsp;/&nbsp;中国大陆&nbsp;/&nbsp;剧情 爱情 同性
  4. </p>
  5. <script>
  6. //[1993,中国大陆,剧情 爱情 同性]
  7. var str = $("p").html().trim().split("<br>")[1].trim();
  8. var reg = /&nbsp;/g
  9. var res = str.replace(reg,"");
  10. var arr = res.split("/");
  11. var last = arr[arr.length-1].split(" ");
  12. console.log(last);
  13. // var sum = [];
  14. // for(var i = 0; i<arr.length; i++){
  15. // if(i<arr.length-1){
  16. // sum.push(arr[i]);
  17. // }
  18. // }
  19. // sum.push(...last);
  20. var sum = [arr[0],arr[1],...last];
  21. console.log(sum);
  22. </script>

7、邮箱验证

  1. <input type="text" placeholder="请输入你的邮箱">
  2. <script>
  3. // 邮箱验证 39775347@qq.com
  4. // cheng_benchao@sina.com
  5. var reg = /^\w+@[a-z]+\.com$/
  6. $("input").keydown(function(event){
  7. if(event.keyCode == 13){
  8. var value = $(this).val();
  9. console.log(value)
  10. console.log(reg.test(value));
  11. }
  12. })
  13. </script>

8、”https://movie.douban.com/subject/129154/

  1. <script>
  2. var url = "https://movie.douban.com/subject/129154/";
  3. var reg = /[^\d]/g
  4. var str = url.replace(reg,"");
  5. var sum = [];
  6. for(var i = 0; i<str.length; i+=2){
  7. var item = str.substr(i,2).split("");
  8. sum.push(item);
  9. }
  10. console.log(sum);
  11. </script>