0

Following up from this thread, im trying to make this work

JavaScript regular expression to match X digits only

string = '2016-2022'
re = /\d{4}/g
result = [...string.matchAll(re)]

This returns an array of two arrays. Is there a way to consolidate this into 1 array?

However it doesn't look like this is returning the desired results

I'm new to regular expression. What am I doing wrong?

Morgan Allen
  • 3,065
  • 5
  • 50
  • 81
  • *This returns an array of two arrays. Is there a way to consolidate this into 1 array?* `[...string.matchAll(re)].flat()`. If the structure is set `'2016-2022'.split('-')` might be an alternative. – Lain Apr 08 '22 at 12:52
  • can you make this the answer? I can mark it as correct – Morgan Allen Apr 08 '22 at 12:55
  • _"Is there a way to consolidate this into 1 array?"_ -> [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users), e.g. _"Yes, you can map or loop through the match results with for-of to build your own array."_ as mentioned on your [previous, quite similar question](https://stackoverflow.com/questions/71797198/javascript-regex-match-4-digits) – Andreas Apr 08 '22 at 13:20
  • @Morgan Allen: Added it for the time being. – Lain Apr 08 '22 at 14:18
  • Does this still need attention? – JavaScript Apr 25 '22 at 08:40

5 Answers5

0

this return an array of matches

result = string.match(re)
Simone Celia
  • 144
  • 1
  • 5
  • 11
0

This is a function to parse the string encoding those two year values and return the inner years as items of an array:

let o = parseYearsInterval('2016-2022');
console.log(o);

function parseYearsInterval(encodedValue){
  var myregexp = /(\d{4})-(\d{4})/;
  var match = myregexp.exec(encodedValue);
  if (match != null) {
    let d1 = match[1];
    let d2 = match[2];
    //return `[${d1}, ${d2}]`;
    let result = [];
    result.push(d1);
    result.push(d2);
    return result;
  } else {
    return "not valid input";
  }
}

I think there are better ways to do that like splitting the string against the "-" separator and return that value as is like:

console.log ( "2016-2022".split('-') )
Diego De Vita
  • 2,262
  • 1
  • 14
  • 23
0

var str = '2016-2022';
var result = [];
str.replace(/\d{4}/g, function(match, i, original) {
    result.push(match);
    return '';
});
console.log(result);

I also wanted to mention, that matchAll does basicly nothing else then an while exec, that's why you get 2 arrays, you can do it by yourself in a while loop and just save back what you need

var result = [];
var matches;
var regexp = /\d{4}/g;
while (matches = regexp.exec('2016-2022')) result.push(matches[0]);
console.log(result);
Scriptkiddy1337
  • 774
  • 4
  • 9
0

Just do a split if you know that only years are in the string and the strucutre isn't changing:

let arr = str.split("-");
John_H_Smith
  • 160
  • 12
0

Question

string = '2016-2022'
re = /\d{4}/g
result = [...string.matchAll(re)]

This returns an array of two arrays. Is there a way to consolidate this into 1 array?

Solution

You may simply flat the result of matchAll.

let string = '2016-2022'
let re = /\d{4}/g
console.log([...string.matchAll(re)].flat())

Alternative

If your structure is given like "yyyy-yyyy-yyyy" you might consider a simple split

console.log('2016-2022'.split('-'))
Lain
  • 3,284
  • 1
  • 17
  • 25