备选字符集

    1. var str ="上海,上天,上哪里去";
    2. var reg =/上[海天]/g
    3. console.log(str.replace(reg,"*")); // *,*,上哪里去
    4. var str = "上海,上天,上哪里"
    5. var reg = /[上天哪]/g
    6. console.log(str.replace(reg,"*")); // *海,**,**里
    7. var res = "ajkdgkbcj128973489"
    8. var reg = /[0-9]/g
    9. console.log(res.replace(reg,"*")); // ajkdgkbcj*********
    10. var str = "hello"
    11. var reg = /[el]o/g
    12. console.log(str.replace(reg,"*")); // hel*

    转义字符

    1. var str = "https://www.baidu.com"
    2. var str2 = "http://www.baidu.com"
    3. var reg = /(http|https):\/\/[w]{3}\.baidu\.com/
    4. console.log(reg.test(str));
    5. console.log(reg.test(str2));

    指定匹配的位置

    1. // 电话
    2. var num = "13897932473123"
    3. var reg = /^1[3-9]\d{9}$/
    4. console.log(reg.test(num));
    1. <input type="text" id="input">
    2. <script>
    3. /* 去除开头结尾的空格 */
    4. var res = []
    5. var reg = /^\s+|\s+$/g;
    6. var input = document.getElementById("input")
    7. input.onkeydown = function(event){
    8. if(event.keyCode == 13){
    9. var str = this.value
    10. res.push(str.replace(reg,""))
    11. console.log(res);
    12. }
    13. }
    14. </script>

    1. var str = "abc123";
    2. var reg = /[^abc]/g;
    3. console.log(str.replace(reg,"*")); //abc***
    4. var test = "?@hello123-";
    5. var re = /[^\d]/g;
    6. console.log(test.replace(re,"*")); //*******123*
    7. var str = "hello210874"
    8. var reg = /[^a-z]/g
    9. console.log(str.replace(reg,"")); // hello
    10. var str = "<p>我是p标签</p>。<div>我是div</div>"
    11. var reg = /<[^<>]+>/g
    12. console.log(str.replace(reg,"")); // 我是p标签。我是div
    13. var str = "https://movie.douban.com/subject/129205/"
    14. var reg = /[^\d+]/g
    15. console.log(str.replace(reg,"")); // 129205

    表单验证

    1. 手机号验证
    2. <input type="text" id="app">
    3. <script>
    4. var reg = /^(086)?1[3-9]\d{9}$/ ;
    5. $("#app").keydown(function(event){
    6. if(event.keyCode == 13){
    7. var value = $(this).val();
    8. console.log(reg.test(value))
    9. }
    10. })
    11. </script>
    1. 身份证号验证
    2. <script>
    3. /*
    4. 42......x
    5. 42900619881024307x
    6. 42\d{15}(\d|x)
    7. 42\d{15}[\dx]
    8. */
    9. var str = "42900619881024307x";
    10. var reg = /^42\d{15}[\dx]$/;
    11. console.log(reg.test(str))
    12. </script>
    1. 邮箱验证
    2. 39775347@qq.com
    3. cheng_chao@sina.com
    4. <script>
    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>

    指定匹配位置

    1. var str = "fdjfj121"
    2. var reg = /^[0-9]+/;
    3. console.log(reg.test(str)) //false
    4. console.log(/\d+$/.test(str)) //true
    5. var str = " hello ";
    6. var reg = /(^\s+)|(\s+$)/g
    7. console.log(str.trim()) //hello
    8. console.log(str.replace(reg,"")) //hello

    获取数字变成数组

    1. <script>
    2. var str = "<span>2395人参观</span>-<span>46人评价</span>"
    3. /*
    4. var arr = [2395,46]
    5. */
    6. var reg = /[^\d-]/g;
    7. var res = str.replace(reg,"");
    8. console.log(res.split("-"))
    9. </script>

    处理文本内容

    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. /* 1993&nbsp;/&nbsp;中国大陆&nbsp;/&nbsp;剧情 爱情 同性 */
    8. var str = $("p").html().trim().split("<br>")[1].trim();
    9. var reg = /&nbsp;/g
    10. var res = str.replace(reg,"")
    11. var arr = res.split("/")
    12. var last = arr[arr.length-1].split(" ");
    13. console.log(last)
    14. var sum = [arr[0],arr[1],...last];
    15. // for( var i=0;i<arr.length;i++){
    16. // if(i<arr.length-1){
    17. // sum.push(arr[i])
    18. // }
    19. // }
    20. // sum.push(...last)
    21. console.log(sum)
    22. </script>
    23. <script>
    24. var url = "https://movie.douban.com/subject/129205/";
    25. /* [[1,2],[9,2],[0,5]] */
    26. var reg = /[^\d]/g;
    27. var str = url.replace(reg,"");
    28. var sum = [];
    29. for(var i=0;i<str.length;i+=2){
    30. var item = str.substr(i,2).split("");
    31. sum.push(item);
    32. }
    33. console.log(sum)
    34. </script>