Reputation: 705
I'd like to be able to split up a string based on a substring delimiter, starting the split before the first character of the delimiter substring. Currently:
var string = "choc: 123 choc: 328 choc: 129";
string.split("choc");
Will give me ["", ":123 ", ": 328", ": 129"]
, but I am looking to get ["choc: 123 ", "choc: 328", "choc: 129"]
, instead.
Upvotes: 0
Views: 297
Reputation: 13993
You can do it with a positive lookahead as @Nina Scholz said, and add \s*
before the lookahead to remove any leading space before choc
:
const string = 'choc: 123 choc: 328 choc: 129';
const result = string.split(/\s*(?=choc)/);
console.log(result);
Upvotes: 0
Reputation: 386883
You could take a positive lookahead.
var string = "choc: 123 choc: 328 choc: 129";
console.log(string.split(/(?=choc)/));
Upvotes: 5