js regular packet & references

Packet

/(\d{4})-(\d{2})-(\d{2})/.test('2019-08-25 ')     // to true 
the RegExp. $. 1 =' 2019 ' 
the RegExp. $ 2 '08 = ' 
the RegExp. $ . 3 = '25' 
the RegExp. $ . 4 = '' 
the RegExp. $ _ = '2019-08-05' // up to RegExp. $ 9, $ 10 is no, which in itself is js pit @ solution: 
'2019-08-25'.match (/ (\ {D}. 4) - (\ D {2}) - (\ D {2}) / );
 // 0: "2019-08-25" 
// . 1: "2019" 
// 2: "08" 
// . 3: "25" 
// Groups: undefined 
// index: 0 
// INPUT: "2019-08-25" 
// length:. 4



ES2018 extension of regular (packet):

'2019-08-25'.match(/(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/)
// 0: "2019-08-25"
// 1: "2019"
// 2: "08"
// 3: "25"
// groups: {year: "2019", month: "08", day: "25"}
// index: 0
// input: "2019-08-25"
// length: 4

 

Quote

'2019-08-25'.match (/ (\ {D}. 4) - (\ D {2}) - \ 2 / )
 // null
 
' 2019-08-08'.match (/ (\. 4 {D }) - (\ D {2}) - \ 2 / )
 // not null 
// last '\ 2' is a reference to the second 

// ES2018 reference 
'2019-08-25'.match (/ (<year> \. 4 {D}?) - (? <month The> \ D {2}) - \ K <month The> /)     // null
 
'2019-08-08'.match (/ (<year>? \. 4 {D}) -? (<month The> \ D {2}) - \ K <month The> /)     // not null
'2019-08-25'.replace (/ (\ {D}. 4) - (\ D {2}) - (\ D {2}) /, `year ($. 1), month The ($ 2 )`)
 // "year (2019), month the (08)" 
// $. 1, $ 2 is a reference to the first two strings match

 

Guess you like

Origin www.cnblogs.com/jingouli/p/11427238.html