Bruno Eduardo Rosselli
Bruno Eduardo Rosselli

Reputation: 167

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);

Upvotes: 0

Views: 137

Answers (1)

Bob
Bob

Reputation: 14654

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);

Upvotes: 2

Related Questions