This little snippet will replace the last comma in a string by the word ” and “. Making it more readable for the end user.
In other words: the string “Other examples are rats, mice,other rodents” will be converted into “Other examples are rats, mice and other rodents”.
var a : Array = ["rats","mice","other rodents"];
var s : String = a.toString(); // returns rats,mice,other rodents
var r : RegExp = /,(?![^,]+,)/g; // the magical formula
s = s.replace( r ," and "); // replace the last , with and
trace( s ); // rats, mice and rodents