游戏屏蔽侮辱性词汇

  1. <script>
  2. var str ="我去,我草,你这游戏打的太牛逼了";
  3. // 脏话 **;
  4. var reg = /[去草]|牛逼/g
  5. console.log(str.replace(reg,"*"))
  6. </script>

input文本框去除两边空白

  1. <script>
  2. var str = " hello ";
  3. var reg = /^\s+|\s+$/g;
  4. var arr = [];
  5. var res = str.replace(reg,"");
  6. arr.push(res);
  7. console.log(arr);
  8. </script>
  1. <script>
  2. /* trim()去除字符串前后的空格
  3. var str = " hello ";
  4. var arr = [];
  5. arr.push(str.trim());
  6. console.log(arr);
  7. </script>

小说网站爬虫过滤标签

  1. <script>
  2. /* 过滤标签*/
  3. var str = "<p>我是</p>。<div>是</div>"
  4. var reg =/<[^<>]+>/g;
  5. console.log(str.replace(reg,""));
  6. </script>

豆瓣网址获取数字段落

  1. <script>
  2. var str = "https://movie.douban.com/subject/1292052/";
  3. var reg = /\d+/;
  4. var res = str.match(reg);
  5. console.log(res[0])
  6. </script>
  1. <script>
  2. var str = "https://movie.douban.com/subject/1292052/";
  3. var reg = /[^\d]/g
  4. console.log(str.replace(reg,""))
  5. </script>

豆瓣获取指定段落

  1. <p id="app">
  2. 导演: 弗兰克·德拉邦特 Frank Darabont&nbsp;&nbsp;&nbsp;主演: 蒂姆·罗宾斯 Tim Robbins /...<br>
  3. 1994&nbsp;/&nbsp;美国&nbsp;/&nbsp;犯罪 剧情
  4. </p>
  5. <script>
  6. // var arr = [1994,"美国","犯罪 剧情"]
  7. /* 1.获取内容,去除字符串前后尾的空格 */
  8. var txt = document.getElementById("app").innerHTML.trim();
  9. /* 2. 通过<br>标签分割字符串为数组,得到数组最后一项,去除前后位空格
  10. 3. 通过/分割字符为数组
  11. */
  12. var arr = txt.split("<br>")[1].trim().split("/");
  13. /* 4.去除数组中每一项 &nbsp; */
  14. var reg = /&nbsp;/g
  15. var res = arr.map(item=>item= item.replace(reg,""));
  16. console.log(res)
  17. </script>

邮箱验证

  1. <input type="text" placeholder="请输入邮箱" id="email">
  2. <script>
  3. var email = document.getElementById("email");
  4. email.onkeyup = function(event){
  5. if(event.keyCode == 13){
  6. var value = this.value.trim();
  7. var reg = /^[\w]+@[0-9a-z]+\.[a-z]+$/;
  8. console.log(reg.test(value));
  9. }
  10. }
  11. /*邮箱的正则
  12. 395775347@qq.com
  13. 395775347@163.com
  14. cheng_benchao@sina.com
  15. chengg@gmail.com
  16. fdfdj@xx.net
  17. */
  18. /*
  19. [\w]+@[0-9a-z]+\.[a-z]+
  20. */
  21. </script>

密码验证

  1. <input type="text" placeholder="请输入密码" id="email">
  2. <script>
  3. var email = document.getElementById("email");
  4. email.onkeyup = function(event){
  5. if(event.keyCode == 13){
  6. var value = this.value.trim();
  7. var reg = /^[a-zA-z]+\d{5,}$/
  8. console.log(reg.test(value))
  9. }
  10. }
  11. /* 请输入6位数密码
  12. 银行密码
  13. var reg = /^\d{6}$/
  14. */
  15. /*
  16. cheng12345 --true
  17. cheng123 --false
  18. 账户密码
  19. 以为字母开头,后面是数字,数字至少包含5位
  20. var reg = /^[a-zA-z]+\d{5,}$/
  21. */
  22. </script>

用户名验证

  1. <input type="text" placeholder="请输入用户名" id="email">
  2. <script>
  3. var email = document.getElementById("email");
  4. email.onkeyup = function(event){
  5. if(event.keyCode == 13){
  6. var value = this.value.trim();
  7. var reg = /^[a-zA-Z]{1,8}$/
  8. console.log(reg.test(value))
  9. }
  10. }
  11. /*
  12. 用户名只能是字母,大小不限,不能超过8个字符
  13. var reg = /^[a-zA-Z]{1,8}$/
  14. */
  15. </script>