To extract the first value from an array result using destructuring, you can use [,...result]
:
const regex = /(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/;
const [,...result] = "1988-02-01 12:12:12".match(regex);
console.log(result)
If you are confident in the input format, a simpler approach is:
result = "1988-02-01 12:12:12".match(/\d+/g)
const regex = /(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/;
const result = "1988-02-01 12:12:12".match(/\d+/g);
console.log(result)
You can add more validation with .match(/(^\d{4}|\b\d{2})\b/g)
const regex = /(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/;
const result = "1988-02-01 12:12:12".match(/(^\d{4}|\b\d{2})\b/g);
console.log(result)
If you need numeric equivalents in the array, use Number
as a callback function with map
:
const regex = /(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/;
const [,...result] = ("1988-02-01 12:12:12".match(regex)||[]).map(Number);
console.log(result)
The use of ||[]
ensures handling cases where no match is found by replacing null
with an empty array.