前端学习——Web API (Day6)
正则表达式
语法
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <script> const str = 你好,振华 // 定义规则 const reg = /你好/ // 检查 console.log(reg.test(str)) </script> </body> </html>
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <script> const str = 你好,振华 // 定义规则 const reg = /你好/ // 检查 console.log(reg.exec(str)) </script> </body> </html>
元字符
边界符
量词
字符类
案例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> span { display: inline-block; width: 250px; height: 30px; vertical-align: middle; line-height: 30px; padding-left: 15px; } .error { color: red; background: url(./images/error1.png) no-repeat left center; } .right { color: green; background: url(./images/right.png) no-repeat left center; } </style> </head> <body> <input type="text"> <span></span> <script> const input = document.querySelector(input) const span = input.nextElementSibling const reg = /^[a-zA-Z0-9-_]{6,16}$/ input.addEventListener(blur,function(){ if(reg.test(this.value)){ span.innerHTML = 输入正确 span.className(right) } else{ span.innerHTML = 请输入6~16位的英文数字下划线 span.className(error) } }) </script> </body> </html>
修饰符
案例
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <textarea name="" id="" cols="30" rows="10"></textarea> <button>发布</button> <div>123</div> <script> const tx = document.querySelector(textarea) const btn = document.querySelector(button) const div = document.querySelector(div) btn.addEventListener(click,function(){ div.innerHTML = tx.value.replace(/激情|基情/g,**) tx.value= }) </script> </body> </html>
综合案例
阶段案例
上一篇:
通过多线程提高代码的执行效率例子