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?
Try this:
This works only for one word strings but that's what I needed:
JSFiddle: https://jsfiddle.net/simo/gou2uhLm/
Seems to work... Tested with the above, "the quick-brown, fox? /jumps/ ^over^ the ¡lazy! dog..." and "C:/program files/some vendor/their 2nd application/a file1.txt".
If you want 2Nd instead of 2nd, you can change to
/([a-z])(\w*)/g
.The first form can be simplified as:
My simple and easy version to the problem:
It's not short but here is what I came up with on a recent assignment in school:
You could immediately
toLowerCase
the string, and then justtoUpperCase
the first letter of each word. Becomes a very simple 1 liner: