People on this site really love to embellish their post titles...
Stewie's sequence: + * - / + * - /
However, when this title needs to be included in the page URL, it is simplified:
stewies-sequence
The Challenge
Your task is to create a program or function which, given a string representing a post title, outputs/returns its "URL-Friendly" conversion.
The algorithm is:
- Convert to lowercase (where applicable)
- Replace every space (
), period (
.
), comma(,
) or slash (/
) with a dash (-
) - Remove any non-alphanumeric characters, except dashes.
- Minify groups of adjacent dashes (
a---b -> a-b
), remove any that are leading/trailing.
Please note that this algorithm is a simplification, and may not always produce the same results as the site's real method.
Rules
- You can assume that input:
- Will not be empty.
- Will contain at least one alphanumeric character.
- Will only contain characters in the ASCII range 32-126 (printable)
- Full programs or functions are permitted.
- A builtin which does the exact task specification is not permitted.
- This is code-golf, so the shortest solution (in bytes) wins!
Test Cases
Most posts on this site will serve as tests, but here's a handy list:
Loading... Forever -> loading-forever
N(e(s(t))) a string -> nest-a-string
"Hello, World!" -> hello-world
URL-Friendly titles -> url-friendly-titles
C.U.S.R.S -> c-u-s-r-s
1+2+3+4+...+n = -1/12? -> 1234-n-1-12
How can I use cmp(a,b) -> how-can-i-use-cmpa-b
Some longer ones...
Export The $PATH Variable, Line-By-Line -> export-the-path-variable-line-by-line
Do n and n^3 have the same set of digits? -> do-n-and-n3-have-the-same-set-of-digits
Quine Anagrams! (Cops' Thread) -> quine-anagrams-cops-thread
The Golfer Adventure - Chapter 1 -> the-golfer-adventure-chapter-1
Bootloader golf: Brainf*** -> bootloader-golf-brainf
And some edge-case checking samples (feel free to suggest more):
0123 -> 0123
a a1 -> a-a1
2-1=1 -> 2-11
-
s? Will they have to be removed? For example inasdf-
, will the last-
have to be removed? \$\endgroup\$if(isalphanum(ch))...
\$\endgroup\$_
underscores? My code works except when there are underscores. \$\endgroup\$