I have a string of an array of arrays,and i need to split this string into an array of arrays.
array = "[['<1>', 'likes'], ['<2>', 'reads'], ['<3>', \"doesn't have\"]]"
this what i've tried so far
array.split(",")
This is the wanted result:
[['<1>','likes'], ['<2>', 'reads'],['<3>', \"doesn't have\"]]
need to split this string into an array of arrays
As Nick Parsons suggested, change your quotations so they are valid JSON, and use JSON.parse()
const array = `[["<1>", "likes"], ["<2>", "reads"], ["<3>", "doesn't have"]]`
console.log(JSON.parse(array))
If you are unable to reformat the input string, you can replace the single quotes with double quotes with this crazy regex that took me surprisingly long to craft:
const array = "[['<1>', 'likes'], ['<2>', 'reads'], ['<3>', \"doesn't have\"]]"
const arrayJSON = array.replace(/(?<=[\[\]\, ])'|'(?=[\[\]\, ])/g, `"`)
console.log(arrayJSON)
console.log(JSON.parse(arrayJSON))