我的要求不仅是大写字符串的首字符,还有一个段落,而且我想像移动设备键盘一样大写每个字符串的首字符。
我尝试了以下解决方案,但不能满足我的要求:
$('#test').blur(function(event) {
var value = $(this).val();
var output = "";
output = value.charAt(0).toUpperCase() + value.slice(1);
});
以上代码仅将首字母大写。
示例输入:
l orem ipsum只是印刷和排版行业的伪文本。自1500年代以来, l orem ipsum一直是业界标准的伪文本,当时一位不知名的打印机拿起一个厨房,将其打乱成一本标本。 t 它!
预期输出:
L orem Ipsum只是印刷和排版行业的伪文本。自1500年代以来, L orem Ipsum一直是行业标准的伪文本,当时一位不知名的打印机拿起一个厨房,将其打乱成一本标本。 T 它!
答案 0 :(得分:2)
您可以使用正则表达式:匹配字符串的开头或句点,后跟空格,然后匹配字母字符,然后使用替换函数在该字符上调用toUpperCase
。要正确替换?
和!
以及.
之后的字母,请使用字符集[.?!]
:
const input = `lorem ipsum is simply dummy text of the printing and typesetting industry. lorem ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.that's it!`;
const cleaned = input.replace(
/(^|\[.?!] *)([a-z])/g,
(_, g1, g2) => g1 + g2.toUpperCase()
);
console.log(cleaned);
要不替换省略号,可以在第一组的.
之前再匹配一个字符:
const input = `foo bar. bar baz... baz buzz? buzz foo`;
const cleaned = input.replace(
/(^|(?:[?!]|[^.]\.) *)([a-z])/g,
(_, g1, g2) => g1 + g2.toUpperCase()
);
console.log(cleaned);
图案
(^|(?:[?!]|[^.]\.) *)([a-z])
表示:
(^|(?:[?!]|[^.]\.) *)
-在第一组中捕获:
^
-字符串的开头,或者:(?:[?!]|[^.]\.)
-匹配上一个句子的结尾:
[?!]
-问号或感叹号,或者[^.]\.
-非句号,后跟句号*
后跟任意数量的空格字符([a-z])
-在第二组中捕获任何字母字符然后
(_, g1, g2) => g1 + g2.toUpperCase()
替换为第一组,将第二组替换为小写。
答案 1 :(得分:1)
尝试
<script>
var input = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.That's it!";
console.log( input.charAt(0).toUpperCase() + input.slice(1))
</script>
答案 2 :(得分:0)
您可以将String#replace()
方法与以下正则表达式匹配:
/\.\s*\w/
与自定义replace回调结合使用,该回调将匹配的字符串大写以实现此目的。这里的基本思想是将紧跟句号(即\.
)后第一个字母的所有子字符串大写。正则表达式还考虑了在句号和下一个字母字符(即\s*
)之间出现零个或多个空格(即\w
)的情况:
var input = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.That's it!";
var result = input.replace(/\.\s*\w/, function(match) {
return match.toUpperCase();
});
console.log('Regular expression based approach', result);
此外,我在评论中注意到您询问了不需要正则表达式的方法。尽管通常会从此类问题中首选使用正则表达式,但以下内容显示了一种非基于re的方法:
const input = `lorem ipsum is simply dummy text of the printing and typesetting industry. lorem ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.that's it!`;
console.log('Non regular expression based approach:',
input
.split('.')
.map((sentence) => {
for (var i = 0; i < sentence.length; i++) {
if (sentence[i] !== ' ') {
sentence = sentence.substr(0, i) + sentence[i].toUpperCase() + sentence.substr(i + 1);
break;
}
}
return sentence
})
.join('.')
)
答案 3 :(得分:0)
假设您的原始输入不包含任何..
,那么下面是一个可行的解决方案:
const raw = `lorem ipsum is simply dummy text of the printing and typesetting industry. lorem ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.that's it!`
const formatted =
raw
.split('.')
.map(sentence => sentence.trim())
.map(sentence => sentence.charAt(0).toUpperCase() + sentence.slice(1))
.join('. ')
console.log(formatted)
// Lorem ipsum is simply dummy text of the printing and typesetting industry. Lorem ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. That's it!
答案 4 :(得分:0)
let paragraph = document.querySelector('#test').textContent;
let result = paragraph.split('.')
.map(sentence => sentence.trim())
.map(sentence => {
sentence = sentence.replace(sentence[0], sentence[0].toUpperCase()) + sentence.slice(1);
return sentence;
});
console.log(result.join('. '));