Home > Article > Web Front-end > Detailed explanation of JS native objects and regular expressions
<script type="text/javascript"> // charAt indexOf replace search subStr // subString splite fromCharCode slice var a = "qwe"; console.log(a); var p = a.indexOf("e"); console.log(p); document.write(String.fromCharCode(72,69,76,76,79));</script>
<script type="text/javascript"> // 创建数组 var arr1 = [1,2,3]; var arr2 = new Array(); // 空数组 var arr3 = new Array(5); // 长度为5 var arr4 = new Array("8"); // ["8"] var arr5 = new Array(4,5); // [4,5] console.log(arr1,arr2,arr3,arr4,arr5); // 属性 console.log(arr5.length); // 方法 // pop push 对数组的末尾添加/删除元素 var newLength = arr5.push(6,7,8); arr5.pop(); console.log(arr5, newLength); // unshift shift 对数组首位添加/删除元素 arr5.unshift(9); console.log(arr5); var arr6 = [1,2,3,4]; // 数组反转 把数组元素的顺序反过来 arr6.reverse(); console.log(arr6); // 把数组转化为字符串 var str = arr6.join("-"); console.log(str); // 排序 arr7 = [99, 45, 78, 22, 11, 1, 123]; arr7.sort(function(a, b){ return a - b; }); console.log(arr7); // splice(index,length, item1,item2.....itemn); // 用后面的item覆盖前面index和length组成的范围 var arr8 = [11, 22, 33, 44]; arr8.splice(2, 0, 999); console.log(arr8); </script>
<script type="text/javascript"> // Date var date = new Date("2015-1-2"); var date2 = new Date("2015-1-1"); console.log(date); console.log(date2); // 两个日期相减得到的是时间差,单位是毫秒 var res = date - date2; console.log(res/1000/3600/24 + 1); // get 获取 console.log(date.getFullYear()); // set 设置 </script>
<script type="text/javascript"> // Math // 属性 console.log(Math.PI); // 方法 // 生成0-1的随机数 Math.random(); // abs 返回绝对值 Math.abs(-3); </script>
<script type="text/javascript"> // reg = /正则部分(定义的规则)/正则属性 // g 全局匹配 i是否区分大小写 var reg = /test/gi var str = "hellotesthowareTESTyoutest"; console.log(str.match(reg)); // \d 代表数字 var reg2 = /\d/g; var str2 = "wfawsefw2390480asdfas"; console.log(str2.match(reg2)); // \w 代表单词字符 数字 字母 下划线 var reg3 = /\w/g; var str3 = "sgfsd234hjl_ sdfj+ \*`134212asd2( ,:a"; console.log(str3.match(reg3)); // .匹配任意字符(除去换行符 \n) var reg4 = /./g; var str4 = "4hjl_ sdfj+ \t*`1342\n12asd2( ,:a"; console.log(str4.match(reg4)); // \s匹配空格字符 匹配空格 var reg5 = /\s/g; console.log(str4.match(reg5)); // 量词 var reg6 = /\d{1,3}/g; var str6 = "3456787re0wr4645"; console.log(str6.match(reg6)); // 方括号 // 2 3 4 6里面的任何一个 var reg7 = /[2-46{2}]/g; var str7 = "23985734985028040"; console.log(str7.match(reg7)); // 除去123以外的任何一个 var reg8 = /[^123]/g; var str8 = "1234567890"; console.log(str8.match(reg8)); // 小括号 var reg9 = /3((4)|(2))/g; var str9 = "2340250123284081"; console.log(str9.match(reg9)); // 开头和结尾 // ^ $ var reg10 = /^34$/g; var str10 = "342093434"; console.log(str10.match(reg10)); // 11位 纯数字 1开头 var reg11 = /^1(3[01379]|47|5[0258])\d{8}$/g; var str11 = "13045340509"; console.log(str11.match(reg11)); // 邮箱 // (4-10位单词字符 开头必须是字母)@ // @qq @163 . cn com var reg12 = /^[A-Z]|[a-z]\w{3,9}@(qq|163)(.com|.cn)$/g; var str12 = "asdfsf@qq.com"; console.log(str12.match(reg12)); </script>
The above is the detailed content of Detailed explanation of JS native objects and regular expressions. For more information, please follow other related articles on the PHP Chinese website!