Skip to content

Instantly share code, notes, and snippets.

@oceangravity
Created March 2, 2016 13:13
Show Gist options
  • Save oceangravity/fa6938c36fc501631cd3 to your computer and use it in GitHub Desktop.
Save oceangravity/fa6938c36fc501631cd3 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
function parse(subselector) {
var selector = [];
subselector.split(/(?=\.\w+)|(?=#\w+)|(?=\[\w+)|(::\w[a-zA-Z0-9!@#$%\^&*)(+=._-]+|:\w[a-zA-Z0-9!@#$%\^&*)(+=._-]+)/gm).forEach(function(token){
console.log(token);
if(token !== undefined){
switch (token[0]) {
case '#':
selector.push('#' + token.slice(1));
break;
case ':':
if(token.match(/::/g)){
selector.push(token);
}else{
selector.push(token);
}
break;
case '.':
selector.push('.' + token.slice(1));
break;
case '[':
selector.push(token.slice(1,-1).split('='));
break;
default :
selector.push(token);
break;
}
}
});
return selector;
}
console.log(parse('div.someClass#id:hover(cu#lo)[key=value]::test[key2=value2].anotherClass'));
</script>
<script id="jsbin-source-javascript" type="text/javascript">
function parse(subselector) {
var selector = [];
subselector.split(/(?=\.\w+)|(?=#\w+)|(?=\[\w+)|(::\w[a-zA-Z0-9!@#$%\^&*)(+=._-]+|:\w[a-zA-Z0-9!@#$%\^&*)(+=._-]+)/gm).forEach(function(token){
console.log(token);
if(token !== undefined){
switch (token[0]) {
case '#':
selector.push('#' + token.slice(1));
break;
case ':':
if(token.match(/::/g)){
selector.push(token);
}else{
selector.push(token);
}
break;
case '.':
selector.push('.' + token.slice(1));
break;
case '[':
selector.push(token.slice(1,-1).split('='));
break;
default :
selector.push(token);
break;
}
}
});
return selector;
}
console.log(parse('div.someClass#id:hover(cu#lo)[key=value]::test[key2=value2].anotherClass'));
</script></body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment