1. var str = "上海,上天,上哪"
    2. var reg = /上[海天哪]/g
    3. console.log(str.replace(reg,"*")); //**,**,**
    4. var s = "你sb,它sx,他sd"
    5. var r = /[A-z]/g
    6. console.log(s.replace(r,"*")); //你**,它**,他**
    1. var str = "it4545e12m3456"
    2. // 4545123456
    3. var reg = /[A-z]/g
    4. console.log(str.replace(reg,""));
    5. 去掉字母
    1. var str = "http://www.baidu.com/sunject/1233/WQW"
    2. var reg = /[A-z0-9]/g
    3. console.log(str.replace(reg,"*"));//****://***.*****.***/*******/****/***
    1. var str = "https://www.baidu.com/123456"
    2. // https://www.baidu.com/
    3. var reg = /[0-9]/g
    4. console.log(str.replace(reg,""));//https://www.baidu.com/
    5. var arr = [
    6. {id:1001,name:"你好3455ffd"},
    7. {id:1001,name:"他家理发店34adasfafd"},
    8. {id:1001,name:"附加费3455asdfd"},
    9. ]
    10. /*
    11. [
    12. {id:1001,name:'你好...'}
    13. ]
    14. */
    15. var r = /[A-z0-9]/g
    16. var res = arr.map(item=>{
    17. var test = item.name.replace(r,"")+"..."
    18. var list = {
    19. [item.id]:test
    20. }
    21. return list
    22. })
    23. console.log(res);
    24. 将原数组处理成[{id:1001,name:'你好...'}]
    1. var phone = "02788667777"
    2. /*
    3. 027?
    4. 8
    5. \d{7}
    6. */
    7. // var reg = /^...$/ 严格匹配 整个字符串必须满足正则
    8. var reg = /^(027)?8\d{7}$/
    9. console.log(reg.test(phone));
    10. 手机号实例
    1. 在输入框输入电话号码,如果格式不正确就弹出输入有误
    2. <input type="text" id="app">
    3. <p style="display: none;">输入有误</p>
    4. <script>
    5. /*
    6. (086)?1[3456789]\d{9}
    7. */
    8. var reg = /^(086)?1[^0-2]\d{9}$/
    9. $('#app').keyup(function(event){
    10. if(event.keyCode === 13){
    11. var value = $(this).val()
    12. console.log(reg.test(value));
    13. if(!reg.test(value)){
    14. $("p").css("display","block")
    15. }
    16. }
    17. })
    1. <input type="text" id="app" placeholder="请输入密码">
    2. <script>
    3. /*
    4. 420983200007136010
    5. 42\d{15}[\dx]
    6. 42\d{15}(\dx)
    7. */
    8. var str = '420983200007136010'
    9. var reg = /^42\d{15}[\dx]$/
    10. console.log(reg.test(str));
    11. /*
    12. 密码
    13. 包含大写小写至少6位
    14. */
    15. // var r = /^[A-z]+\d{6,}$/
    16. var r = /^[A-z+\d]{6,}$/
    17. $('#app').keyup(function(event){
    18. if(event.keyCode === 13){
    19. var value = $(this).val()
    20. console.log(r.test(value));
    21. }
    22. })
    23. </script>
    24. 身份证号实例
    1. var str = "<span>2395人参观</span>-<span>46人评价</span>"
    2. // var arr = [2395,45]
    3. var reg = /[^\d-]/g
    4. var arr = []
    5. arr.push(str.replace(reg,"").split("-"))
    6. console.log(arr);//var arr = [2395,45]
    7. 筛选旧数组获取新的数组
    1. <p class="app">
    2. 导演: 陈凯歌 Kaige Chen&nbsp;&nbsp;&nbsp;主演: 张国荣 Leslie Cheung / 张丰毅 Fengyi Zha...<br>
    3. 1993&nbsp;/&nbsp;中国大陆 中国香港&nbsp;/&nbsp;剧情 爱情 同性
    4. </p>
    5. <script>
    6. var value = $('.app').html().trim().split("<br>")[1].trim()
    7. // [1993,中国大陆,剧情,爱情,同性]
    8. var reg = /&nbsp;|中国香港/g
    9. console.log(value.replace(reg,""));
    10. var res = value.replace(reg,"").split("/")
    11. var list = res[res.length-1].split(" ")
    12. var sum = []
    13. for(var i=0;i<res.length;i++){
    14. if(i<res.length-1){
    15. sum.push(res[i])
    16. }
    17. }
    18. sum.push(...list)
    19. console.log(sum);
    20. </script>
    21. 获取一个这样的[1993,中国大陆,剧情,爱情,同性]数组
    1. <input type="text" id="app" placeholder="请输入你的邮箱">
    2. <script>
    3. /*
    4. 邮箱验证
    5. 2820163916@qq.com
    6. cheng_dong@sina.com
    7. */
    8. var reg = /^\w+@[a-z]+\.com$/
    9. $('#app').keyup(function(event){
    10. if(event.keyCode === 13){
    11. var value = $(this).val()
    12. console.log(reg.test(value));
    13. }
    14. })
    15. </script>
    16. 邮箱验证
    1. var str = "<p>冯大大发</p><br><span>666</span>"
    2. var reg = /<[^<>]+>/g
    3. console.log(str.replace(reg,""));
    4. 获取标签内的文本
    1. var url = "https://movie.douban.com/subject/129205/"
    2. // [[1,2],[9,2],[0,5]]
    3. var reg = /[^\d]/g
    4. var res = url.replace(reg,"")
    5. var arr = res.split("")
    6. var test = []
    7. for(var i=0;i<arr.length;i+=2){
    8. test.push(arr.slice(i,i+2))
    9. }
    10. console.log(test);
    11. 获取url字符串里的数字,并将其拆分成二维数组