Skip to content

Instantly share code, notes, and snippets.

@jmealo
Created January 30, 2013 14:46
Show Gist options
  • Save jmealo/4673750 to your computer and use it in GitHub Desktop.
Save jmealo/4673750 to your computer and use it in GitHub Desktop.
This function returns the capitalized abbreviation of the passed string according to APA rules for title case (http://blog.apastyle.org/apastyle/2012/03/title-case-and-sentence-case-capitalization-in-apa-style.html). It omits the following minor words: and, or, nor, but, a, an, the, as, at, by, for, in, of, on, per and to.
function strToAbbr(str) {
str = str.toUpperCase();
str = str.replace(/\b(AND|OR|NOR|BUT|A|AN|THE|AS|AT|BY|FOR|IN|OF|ON|PER|TO)\b/g, '');
var words = str.split(/\s+/), abbr = '';
for(var x = 0; x < words.length; x++) abbr += words[x].substr(0,1);
return abbr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment