将JavaScript / Jquery中的第一个字母大写 [英] Capitalize the first letter of sentense in JavaScript/Jquery

查看:177
本文介绍了将JavaScript / Jquery中的第一个字母大写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是测试。这是另一个测试。这是新的测试。

This is test. This is one more test. And this is new test.

推荐答案

你不需要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());

这篇关于将JavaScript / Jquery中的第一个字母大写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆