Skip to content

Instantly share code, notes, and snippets.

@mjkaufer
Created October 5, 2014 23:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjkaufer/8b88ff48cb6ef5ea6ca6 to your computer and use it in GitHub Desktop.
Save mjkaufer/8b88ff48cb6ef5ea6ca6 to your computer and use it in GitHub Desktop.
Converts lists to prettier lists. I.e. `data1, data2, data3, ` to `data1, data2, and data3`
var data = "";
for(var i = 0; i < 10; i++){//this is to give default values to the data set, to emulate what it would look like
data+=i + ", ";
}
data = data.replace(/\, $/g,"");//take out trailing comma
data = data.replace(/,([^,]*)$/, ", and$1");//adds a pretty "and" to the end
console.log(data);
//voila
//before
//"0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "
//after
//"0, 1, 2, 3, 4, 5, 6, 7, 8, and 9"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment