Files
30-seconds-of-code/snippets/kebab.md
Angelos Chalaris 53e4e7f63d Remove semicolons, update imports
Remove semicolons from camel, update imports
Remove semicolons in kebab, update imports
2020-01-03 13:08:28 +02:00

27 lines
638 B
Markdown

---
title: kebab
tags: string,regexp,intermediate
---
Converts a string to kebab case.
Break the string into words and combine them adding `-` as a separator, using a regexp.
```py
from re import sub
def kebab(s):
return sub(
r"(\s|_|-)+","-",
sub(
r"[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+",
lambda mo: mo.group(0).lower(), s))
```
```py
kebab('camelCase') # 'camel-case'
kebab('some text') # 'some-text'
kebab('some-mixed_string With spaces_underscores-and-hyphens') # 'some-mixed-string-with-spaces-underscores-and-hyphens'
kebab('AllThe-small Things') # "all-the-small-things"
```