Skip to content

Instantly share code, notes, and snippets.

@mistercoffee66
Last active November 2, 2020 22:43
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 mistercoffee66/6bfdaf438e8434ab05750279529bc5d9 to your computer and use it in GitHub Desktop.
Save mistercoffee66/6bfdaf438e8434ab05750279529bc5d9 to your computer and use it in GitHub Desktop.
trim anything
export const trimAnything = (str, trimChar, opts = { start: false, end: true }) => {
if (str.length > 1 || opts.allowEmpty) {
let pattern;
if (opts.start && !opts.end) {
pattern = `^\\${trimChar}+`;
} else if (!opts.start && opts.end) {
pattern = `\\${trimChar}+$`;
} else {
pattern = `^\\${trimChar}+|\\${trimChar}+$`;
}
return str.replace(new RegExp(pattern, 'g'), '');
}
return str;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment