Skip to content

Instantly share code, notes, and snippets.

@filipesperandio
Last active July 19, 2019 19:32
Show Gist options
  • Save filipesperandio/897e562ae38c53ac2543c280767fb974 to your computer and use it in GitHub Desktop.
Save filipesperandio/897e562ae38c53ac2543c280767fb974 to your computer and use it in GitHub Desktop.
function joinEndWith(arr, separator, last) {
const reg = new RegExp(`(.+)${separator}(.+)`)
return arr.join(separator).replace(reg, `$1${last}$2`)
}
@filipesperandio
Copy link
Author

joinEndWith([1,2,3], ", ", " and ")
// "1, 2 and 3"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment