1.regexp
var str = 'dftd5rw5e1231211yasgdafdt12wq12fw1ft';
var pattern = /[daf]/ig;
var pattern = /[daf]{2}/ig;
//
var pattern = /[^daf]/ig;
//
var pattern = /^[a-z].*\d$/g;
var str = 'a29';
var pattern = /^\d{11}$/;
//手机号
var pattern = /^([a-z]|[0-9]){3,12}@([a-z]|[0-9]){2,10}\.[a-z]{3,5}$/ig;
var str = '1258392784@qq.com';
//匹配邮箱
console.log(str.match(pattern));
var str = 'ababa';
var pattern = /([a-z]){2}.\1/g;
console.log(str.match(pattern));
//abab
//如果匹配上直接抛出,没有匹配上从下一个继续开始
2.包装器
//Boolean Number String
var n = new Number(123);
console.log(n);//[number:123]
console.log(typeof n);//object
console.log(n instanceof Number);//true
console.log(n instanceof Object);//true
var m = 123;
console.log(m);//123
console.log(typeof m);//number
console.log(m instanceof Number);//false
console.log(m instanceof Object);//false
var o = new Object(123);
console.log(o);//[number:123]
console.log(o instanceof Number);//true
console.log(o instanceof Object);//true
//基本类型到包装器类型转换 new
3.String
//length 字符串长度/个数
//charAt(index)获取index 位置上的字符
//charCodeAt(index) index ASCII
var str = ' ys a uc a cy c';
str.split('').forEach(function(item){
console.log(item);
});
for(i=0;i<str.length;i++){
console.log(str.charAt(i),'--',str.charCodeAt(i));
}
console.log(str.split('').join(''));
//删除所有空格
// console.log(str.trim());
//去首尾空格
console.log(str.concat('hello'));
console.log(str.concat({name:'123'}));
console.log(str.toUpperCase());
//全大写
console.log(str.toLowerCase());
//全小写
4.Math
var num = 12.3;
console.log(Math.ceil(num));
//向上取13
console.log(Math.floor(num));
console.log(Math.parseInt(num));
//向下取12
console.log(Math.round(num));
//四舍五入取整
console.log(Math.random());
//前世今生
var arr=['李元霸','李白','王昭君','貂蝉','西施','项羽','虞姬'];
var index = Math.floor((Math.random()*arr.length));
console.log(arr[index]);
//ES5
console.log(Math.min(23,45,32,65,18,9));//9
console.log(Math.max(45,65,35,32,75,34));//75
练习正则表达式
最新推荐文章于 2024-04-12 14:26:02 发布