Last active
August 29, 2015 14:15
-
-
Save A/a62d795297afca62e35f to your computer and use it in GitHub Desktop.
convert string to array of ordinary strings and key-named strings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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