Skip to content

Split does not separate string

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.

let value = "9月 28, 2021";
let result = value.split(' ');
console.log(result);

Answer

You can get the expected result by spliting word boundaries b in addition to spaces, and commas.

let value = "9月 28, 2021";
let result = value.split(/b[s,]*|[s,]*b/g);
console.log(result);