const str = '123, 456, 789,123,456,789,123' const re =/(?:[^,]+,){3}/g alert(str.replace(re, '$&\n'))