How do I make the first letter of a string uppercase in JavaScript?

前端 未结 30 2335
南方客
南方客 2020-11-21 05:00

How do I make the first letter of a string uppercase, but not change the case of any of the other letters?

For example:

  • \"this is a test\"
30条回答
  •  我寻月下人不归
    2020-11-21 05:19

    There is a very simple way to implement it by replace. For ECMAScript 6:

    'foo'.replace(/^./, str => str.toUpperCase())
    

    Result:

    'Foo'
    

提交回复
热议问题