indices-array-matched.js (1514B)
1 // Copyright 2019 Ron Buckton. All rights reserved. 2 // This code is governed by the BSD license found in the LICENSE file. 3 4 /*--- 5 description: The properties of the "indices" array correspond to the start/end indices of the same values in the match. 6 includes: [compareArray.js] 7 esid: sec-makeindicesarray 8 features: [regexp-match-indices] 9 info: | 10 MakeIndicesArray ( S, indices, groupNames, hasIndices ) 11 4. Let _n_ be the number of elements in _indices_. 12 ... 13 8. Set _A_ to ! ArrayCreate(_n_). 14 ... 15 13. For each integer _i_ such that _i_ >= 0 and _i_ < _n_, do 16 a. Let _matchIndices_ be _indices_[_i_]. 17 b. If _matchIndices_ is not *undefined*, then 18 i. Let _matchIndicesArray_ be ! GetMatchIndicesArray(_S_, _matchIndices_). 19 c. Else, 20 i. Let _matchIndicesArray_ be *undefined*. 21 d. Perform ! CreateDataProperty(_A_, ! ToString(_n_), _matchIndicesArray_). 22 ... 23 ---*/ 24 25 let input = "abcd"; 26 let match = /b(c)/d.exec(input); 27 let indices = match.indices; 28 29 // `indices` has the same length as match 30 assert.sameValue(indices.length, match.length); 31 32 // The first element of `indices` contains the start/end indices of the match 33 assert.compareArray(indices[0], [1, 3]); 34 assert.sameValue(input.slice(indices[0][0], indices[0][1]), match[0]); 35 36 // The second element of `indices` contains the start/end indices of the first capture 37 assert.compareArray(indices[1], [2, 3]); 38 assert.sameValue(input.slice(indices[1][0], indices[1][1]), match[1]); 39 40 reportCompare(0, 0);