Skip to content

Instantly share code, notes, and snippets.

@fearthecowboy
Last active February 12, 2020 17:47
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 fearthecowboy/39c537909d50acc412b587c0d68ddd9e to your computer and use it in GitHub Desktop.
Save fearthecowboy/39c537909d50acc412b587c0d68ddd9e to your computer and use it in GitHub Desktop.
Split identifier into words
const identifier = "SomethingNOTGarrettButTLAsInASetOfEverLongerWordsTLAWithIsOfAPIsAPIssNotNOManThatYouUseWith_a_TLA";
// split identifier into words
identifier.replace(/([a-z]+)([A-Z])/g, '$1 $2').
replace(/(\d+)/g, ' $1 ').
replace(/\b([A-Z]+)([A-Z])s([^a-z])(.*)/g, '$1$2« $3$4').
replace(/\b([A-Z]+)([A-Z])([a-z]+)/g, '$1 $2$3').
replace(/«/g, 's').trim().
split(/[\W|_]+/)
// returns:
// ['Something','NOT','Garrett','But','TLAs','In','A','Set','Of','Ever',
// 'Longer','Words','TLA','With','Is','Of','AP','Is','AP','Iss','Not',
// 'NO','Man','That','You','Use','With','a','TLA'] 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment