-2

it's a simplified version mac address, I want to match the numbers to an array, the following code's not working as expected. match result only contains the last match '66'.

const matches = '11:22:33:44:55:66'.matchAll(/^((\d{2}):?){6}$/g); console.log([...matches]);

1 Answers1

1

Why not

const matches = '11:22:33:44:55:66'.split(":")
console.log(matches);

// If you need to extract from a string
const mac = 'Here is my mac address: 11:22:33:44:55:66'.match(/((\d{2}):?){6}/g)[0]?.split(":");
console.log(mac)
mplungjan
  • 155,085
  • 27
  • 166
  • 222
  • 1
    the real string is a little more complicated than this, it cannot be easily split. so the regex way is the only way I'm looking for. – thousands_sun Jun 01 '22 at 08:40
  • See second example - always a good idea to give as much details as possible – mplungjan Jun 01 '22 at 08:41
  • thanks. I can do it with more than one step. [link](https://stackoverflow.com/questions/3537878/how-to-capture-an-arbitrary-number-of-groups-in-javascript-regexp) Now I realize it's impossible to archieve my goal with only one step. because I just did this with C# days ago, so I think js can do the same thing. – thousands_sun Jun 01 '22 at 09:03