When breaking lines, the easiest way to avoid corner cases is to add a separator to the line when you know that the line cannot end with a separator:
str = "a,b,c,d,e,f,g"
str = str .. ','
for w in str:gmatch("(.-),") do print(w) end
Alternatively, you can use a template with an extra separator:
str = "a,b,c,d,e,f,g"
for w in str:gmatch("([^,]+),?") do print(w) end
, -:
str = "a,b,c,d,e,f,g"
for w in str:gmatch("([^,]+)") do print(w) end