1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <title>Document</title>
    8. </head>
    9. <body>
    10. <script>
    11. //1.方式1:使用字面量方式创建对象
    12. var reg = /xinlang/;
    13. var str = 'fsafafasfxinlangfafafa';
    14. //检验字符是否匹配正则表达式,用test()
    15. var boo = reg.test(str);
    16. console.log(boo);
    17. //2.方式2:使用构造函数方式创建对象
    18. var reg2=new RegExp('zhangsan');
    19. var str='dasfasgfasfzhangsanfafa';
    20. var boo2=reg2.test(str);
    21. console.log(boo2);
    22. //3.正则表达式参数g-全局匹配,i-忽略大小写,m-多行匹配
    23. var reg3 = /zhanGsan/gi;
    24. // var reg3 = new RegExp('zhangsan','g');
    25. var str3 = '11111111zhangsan2132314141zhangsan';
    26. var newStr3 = str3.replace(reg3, '张三');
    27. console.log(newStr3);
    28. </script>
    29. </body>
    30. </html>