0
How to split String with pattern [123, 556, 9898] into arrays, in JavaScript
For example a String: str = "[123, 556, 9898]" Into an Array: arr = ["123", "556" ,"9898"];
4 odpowiedzi
+ 4
let str = "[123, 234, 467]"
str = str.replace(/(\[|\])/g, "").split(", ")
console.log(str);
+ 3
What i did:
1) removed [ and ] from string using regex, but u can also do it without regex:
str = str.replace("[" "")
str = str.replace("]", "")
2) I splitted the array with ", "
+ 1
Are you sure that string has those square brackets?
You could use String.split() method
arr = str.split(",");
0
yes, string has brackets, and they should be avoided.