String pattern matching, what is the result of executing the following code? And the reasons.

related codes

var colorText = "red,blue,green,yellow";
colorText.split(/[^\,]+/);
// ["", ",", ",", ",", ""]
Jul.06,2022

regularly matches non-comma content. Split uses the matching content as a knife to cut the string into two halves


there is actually an empty string before red. Similarly, there is an empty string
after yellow, and your regular character is separated by characters other than comma [1,], so three commas plus two empty strings

Menu