Is there a simple way to convert a string to title case? E.g. john smith
becomes John Smith
. I'm not looking for something complicated like John Resig's solution, just (hopefully) some kind of one- or two-liner.
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
If regex used in the above solutions is getting you confused, try this code:
Try this, shortest way:
Use
/\S+/g
to support diacritics:However: "sunshine (yellow)" ⇒ "Sunshine (yellow)"
Here's my version, IMO it's easy to understand and elegant too.
If you can use third party libraries in your code then lodash has a helper function for us.
https://lodash.com/docs/4.17.3#startCase
Just in case you are worried about those filler words, you can always just tell the function what not to capitalize.
Hope this helps you out.