var exp = new RegExp(pattern,modifier) //pattern為正則的條件或者是字符串;
modifier為修飾符 g : 表示全局匹配;i:忽略大小寫;m:忽略大小寫;^:以什么開頭;$:以什么結尾
var exp = new RegExp("h",g);
var str = 'hello word';
exp.test(str) //返回的是布爾值 true
exp.test(str) //返回的是布爾值 false
exp.exec(str) //返回的是數組:["h", index: 0, input: "hello word", groups: undefined]
exp.exec(str)//返回的是null
注意:test,exec方法第一次是從第一個元素開始找,第二次是從上一次找到的位置開始找 lastIndex
lastIndex為下一次查找的索引值,如果沒有查找到下一次就重置為0;
如果想要每次都是從第一個開始找就需要重置lastIndex;
exp.lastIndex = 0;
注意 這種情況只存在於全局查找時;
str.macth(exp) //返回的是數組:["h"];
str.replace(exp)//返回字符串 fello word;