https://blog.csdn.net/liangklfang/article/details/49246135
//全局匹配下
var reg = /(\d)/g;
do {
m = reg.exec('123');
console.log(m);
} while (m)
console.log('123'.match(reg));
//exec輸出
Array(2)0: "1" 1: "1" groups: undefined index: 0 input: "123" length: 2
Array(2)0: "2" 1: "2" groups: undefined index: 1input: "123" length: 2
Array(2)0: "3" 1: "3" groups: undefined index: 2 input: "123" length: 2
//match輸出
(3) ["1", "2", "3"]
0:"1"
1:"2"
2:"3"
length:3
結論:
在全局匹配模式下,match返回一個所有匹配項的數(shù)組,而exec只會返回第一個匹配項數(shù)組,數(shù)組內(nèi)容包括匹配項內(nèi)容,第一個捕獲組.....第n個捕獲組,匹配的位置index,所驗證匹配的字符串input,長度length。
在非全局匹配模式下,match返回第一個匹配項數(shù)組,exec也返回第一個匹配項數(shù)組,兩者內(nèi)容一致,數(shù)組包括匹配項內(nèi)容,第一個捕獲組.....第n個捕獲組,匹配的位置index,所驗證匹配的字符串input,長度length