如何将 string 之后的所有字符大写。像手机键盘
How to capitalize all the chars of string after . like mobile keyboard
我的要求不仅是字符串的第一个字符大写,还有一个段落,我想像移动设备键盘一样将每个字符串的第一个字符大写。
我已经尝试了以下解决方案,但不能满足我的要求:
$('#test').blur(function(event) {
var value = $(this).val();
var output = "";
output = value.charAt(0).toUpperCase() + value.slice(1);
});
以上代码仅将第一个字符大写。
示例输入:
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!
预期输出:
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!
您可以使用正则表达式:匹配字符串的开头或后跟 space 的句点,然后匹配字母字符,并使用替换函数调用 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])
表示:
(^|(?:[?!]|[^.]\.) *)
- 在第 1 组捕获:
^
- 字符串的开头,或者:
(?:[?!]|[^.]\.)
- 匹配上一句的结尾:要么
[?!]
- 问号或感叹号,或
[^.]\.
- 非句号,后跟句号
*
后跟任意数量的 space 个字符
([a-z])
- 在第二组中捕获任何字母字符
然后
(_, g1, g2) => g1 + g2.toUpperCase()
替换为第一组,并与小写的第二组连接。
您可以将 String#replace()
方法与以下正则表达式匹配:
/\.\s*\w/
结合将匹配字符串大写的自定义替换回调来实现这一点。这里的基本思想是将紧跟在句号后面的第一个字母的任何子字符串大写(即 \.
)。正则表达式还考虑了在句点和下一个字母字符(即 \w
)之间出现零个或多个空格(即 \s*
)的情况:
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('.')
)
试试这个
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))
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('. '));
我的要求不仅是字符串的第一个字符大写,还有一个段落,我想像移动设备键盘一样将每个字符串的第一个字符大写。
我已经尝试了以下解决方案,但不能满足我的要求:
$('#test').blur(function(event) {
var value = $(this).val();
var output = "";
output = value.charAt(0).toUpperCase() + value.slice(1);
});
以上代码仅将第一个字符大写。
示例输入:
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!
预期输出:
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!
您可以使用正则表达式:匹配字符串的开头或后跟 space 的句点,然后匹配字母字符,并使用替换函数调用 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])
表示:
(^|(?:[?!]|[^.]\.) *)
- 在第 1 组捕获:^
- 字符串的开头,或者:(?:[?!]|[^.]\.)
- 匹配上一句的结尾:要么[?!]
- 问号或感叹号,或[^.]\.
- 非句号,后跟句号
*
后跟任意数量的 space 个字符
([a-z])
- 在第二组中捕获任何字母字符
然后
(_, g1, g2) => g1 + g2.toUpperCase()
替换为第一组,并与小写的第二组连接。
您可以将 String#replace()
方法与以下正则表达式匹配:
/\.\s*\w/
结合将匹配字符串大写的自定义替换回调来实现这一点。这里的基本思想是将紧跟在句号后面的第一个字母的任何子字符串大写(即 \.
)。正则表达式还考虑了在句点和下一个字母字符(即 \w
)之间出现零个或多个空格(即 \s*
)的情况:
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('.')
)
试试这个
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))
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('. '));