不加括號進行分組時數組就只有"12324"一項,加了括號為什么變成兩個?
str.match()中匹配的正則分組問題
夢里花落0921
2018-10-03 10:15:23
TA貢獻1780條經驗 獲得超1個贊
match方法的匹配結果為一個數組。這個數組分為4部分,依次為:
整個正則表達式匹配的字符串
括號匹配的子串,每一對括號對應一個子串
index:匹配的第一個字符位置
input:輸入字符串
str = "12324"str.match(/\d+/) [ '12324', index: 0, input: '12324' ]
str = "12324"str.match(/(\d+)/) [ '12324', '12324', index: 0, input: '12324' ]
str = "123abc" str.match(/\d+[a-z]+/) [ '1232abc', index: 0, input: '1232abc' ]
str = "1232abc"str.match(/(\d+)([a-z]+)/) [ '1232abc', '1232', 'abc', index: 0, input: '1232abc' ]
舉報