I need to break a string into several parts and for that I did a split, but the split doesn’t break the string. It returns an array with only one value and my string inside [“9月 28, 2021”] I expected it to return an array with [9], [月], [28], [2021]. I think JS gets lost with the 月 character, I honestly don’t know what to do.
JavaScript
x
3
1
let value = "9月 28, 2021";
2
let result = value.split(' ');
3
console.log(result);
Advertisement
Answer
You can get the expected result by spliting word boundaries b
in addition to spaces, and commas.
JavaScript
1
3
1
let value = "9月 28, 2021";
2
let result = value.split(/b[s,]*|[s,]*b/g);
3
console.log(result);