Is it possible to explode a string using different operators?
I am trying to extract every code (of varying sizes) between the brackets [
and ]
Here are some examples of the different possibilities:
const codes = [
'[5018902847][592][50189272809][5089113805]',
'[3898]',
'[375001833][3475001021]',
'',
]
And this is what I need:
[5018902847, 592, 50189272809, 5089113805]
[3898]
[375001833, 3475001021]
null // because code is empty
My initial solution looks like this :
codes.map(code => {
const res = code.split('[ ' || '][' || ']');
console.log(res)
})
However, the result does not match my expectation, for example:
[
' [5',
'51889',
'28475][5',
'51889424192][5',
'518892728',
'9][5',
'518891138',
'5]'
]