Skip to content

Instantly share code, notes, and snippets.

@alexweininger
Created August 12, 2021 23:55
Show Gist options
  • Save alexweininger/8ee33e9967e73e7a7ed3b76a3265a679 to your computer and use it in GitHub Desktop.
Save alexweininger/8ee33e9967e73e7a7ed3b76a3265a679 to your computer and use it in GitHub Desktop.
Trim quotes from a string
export function trimQuotes(str: string): string {
str = str.trim();
const arr = Array.from(str);
const quotationMark = '"';
const first = arr.findIndex(char => char !== quotationMark);
const last = arr.reverse().findIndex(char => char !== quotationMark);
return (first === -1 && last === -1) ? '' : str.substring(first, str.length - last);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment