且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将JavaScript / Jquery中的第一个字母大写

更新时间:2023-02-05 13:45:38

你不需要jQuery。您可以使用Javascript 字符串数组函数。

You don't need jQuery for this. You can use Javascript string and array functions.


  1. 分割字符串。,分隔不同的句子

  2. 修剪每个句子

  3. 首字母大写字母

  4. 加入。

  1. Split the string by ., to separate different sentences
  2. Trim each sentence
  3. Capitalize first letter
  4. Join by .

var str = 'this is a test. this is one more test. and this also new test.';

var newStr = str.split('.').map(function(el) {
  el = el.trim();
  return el.substr(0, 1).toUpperCase() + el.substr(1);
}).join('. ');

alert(newStr.trim());