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

前端 未结 30 2222
南方客
南方客 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:15

    yourString.replace(/^[a-z]/, function(m){ return m.toUpperCase() });
    

    (You may encapsulate it in a function or even add it to the String prototype if you use it frequently.)

提交回复
热议问题