From comments I understand you don't want to split at each comma or semi-colon, but only when the maximum size is about to be reached. Also you want to keep the delimiter (the comma or semi-colon where you split at) in the result.
To add a max-size limit to the regular expression, you can use a regex like .{1,100}
, where 100 is that maximum (for example). If your engine does not support the dotAll flag (yet), then use [^]
instead of .
to ensure that even newline characters are matched here.
To ensure that the split happens just after a delimiter, add (.$|[,;])
to the regex, and reduce the previous {1,100}
to {1,99}
.
Then there is the case where there is no delimiter in a substring of 100 or more characters: the following code will choose to then exceptionally allow a longer chunk, until a delimiter is found. You may want to add white space (\s
) as a possible delimiter too.
Here is a function that takes the size as argument and creates the corresponding regex:
const mySplit = (s, maxSize=s.length) => s.match(new RegExp("(?=\\S)([^]{1," + (maxSize-1) + "}|[^,;]*)(.$|[,;])", "g"));
console.log(mySplit("hello,this is a longer sentence without commas;but no problem", 20));