I am trying to write a custom string splitting function, and it is harder than I would have expected.
Basically, I pass in a string and an array of the values that the string will split on, and it will return an array of substrings, removing empty ones and including the values it splits on. If the string can be split at the same place by two different values, the longer one has precedence.
That is,
split("Go ye away, I want some peace && quiet. & Thanks.", ["Go ", ",", "&&", "&", "."]);
should return
["Go ", "ye away", ",", " I want some peace ", "&&", " quiet", ".", " ", "&", " Thanks", "."]
Can you think of a reasonably simple algorithm for this? If there is a built-in way to do this in Javascript (I don’t think there is), it would be nicer.
Something like this?