Skip to content

Instantly share code, notes, and snippets.

@A
Last active August 29, 2015 14:15
Show Gist options
  • Select an option

  • Save A/a62d795297afca62e35f to your computer and use it in GitHub Desktop.

Select an option

Save A/a62d795297afca62e35f to your computer and use it in GitHub Desktop.
convert string to array of ordinary strings and key-named strings
// convert string to array of ordinary strings and key-named strings
function splitByKeys(pattern) {
let regexp = /(:\w+)/g;
return pattern
.split(regexp)
// remove trailings
.filter(function(str) { return !!str; })
;
}
splitByKeys('/topic/:date/:name'); // ['/topic/', ':date', '/', ':name']
splitByKeys('/topic/:slug/2015/02/12/:name'); // ['/topic/', ':slug', '/2015/02/12/', ':name']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment