js基本语法2
正则表达式
var reg1=/\d/;
var reg2=new RegExp("\d");//对象的方式
小写 \d \D大写取反,任意一个非数值
\d 匹配的是0-9任意数字
[] 匹配的是其中的任意一个字符(数字和其它特殊符号) [0-9]\d
12=>[12] 匹配的是1或2 12=>[1][2] 12=>12
[12a] 1,2,a
[^] 非其中的一个字符 [^0-9]
\w 数字、字母、下划线 [a-zA-Z0-9_]
. 匹配任意一个字符
[.] 匹配的是 .
| 或
2|3 匹配的是2、3
? 0-1次
0?[1-9]
+ 至少一次 1-多次
* 0-多次
{1,4} 最少,最多
^ 开始
$ 结束
创建对象
//字面量 json 第一种方式
var student={
id:10001,
name:"张三",
scores:[
{student:"html",scores:90},
{student:"js",scores:90},
]
}
//function 第二种方式
function Student(id,name){
this.id=id;
this.name=name;
this.scores=[
{student:"html",scores:90},
{student:"js",scores:90}
]
}
//扩展
Student.prototype.sex="男";
Student.prototype.eat=function(food){
console.log("吃"+food)
}
var stu=new Student(10001,"张三");
stu.eat("米饭");
console.log(stu.sex);
// object 第三种方式
var stu2=new Object();
stu2.id=1000;
stu2.name="张三";
stu2.scores=[
{student:"html",scores:90},
{student:"js",scores:90}
]
// 链式编程
function Student(id,name){
this.id=id;
this.name=name;
this.eat=function(food){
console.log("吃"+food)
return this
},
this.sleep=function(){
console.log("睡")
return this
}
}
var stu=new Student(10001,"张三");
stu.eat("").sleep().eat("").sleep().eat("").sleep()
浙公网安备 33010602011771号