Skip to content

Instantly share code, notes, and snippets.

@mateusvahl
Created August 21, 2017 14:20
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 mateusvahl/1c90f217308dd42005a3545f534ec59e to your computer and use it in GitHub Desktop.
Save mateusvahl/1c90f217308dd42005a3545f534ec59e to your computer and use it in GitHub Desktop.
const parseElementExpression = (str) => {
const elements = /([\#|\.][a-zA-Z]+)/;
const itens = str.split(elements).filter(x => x);
const getClassOrIdNames = (idOrClass) =>
itens
.filter(_ => _.charAt(0) === idOrClass)
.map(_ => _.slice(1));
const tag = itens.filter(_ => (_.charAt(0) !== '#') && (_.charAt(0) !== '.'));
const id = getClassOrIdNames('#');
const classes = getClassOrIdNames('.');
const attrs = {id: id, 'class': classes};
return [tag, attrs];
}
const [tag, attrs] = parseElementExpression("div#hellow.xyz.abc");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment