你能看看这段代码吗。我需要解决这个案子的选项。每个句子的第一个字母应该大写。
这是测试。这是测试。这是测试。
问题是,只有第一句以大写字母开头,而其他句子则没有。
https://github.com/freewarelovers/CaseConverter
// LowerCase, Title And Sentence Case Converter Tool
var stringbox = document.getElementById('stringbox')
var wordcountspan = document.getElementById('wordcount')
var charcountspan = document.getElementById('charcount')
function convertstring(textarea, action) {
if (action == 'sentencecase') {
textarea.value = sentenceCase(textarea.value)
} else if (action == 'titlecase') {
textarea.value = toTitleCase(textarea.value)
} else if (action == 'capitalcase') {
textarea.value = CapitalCase(textarea.value)
} else if (action == 'lowercase') {
textarea.value = lowerCase(textarea.value)
} else if (action == 'uppercase') {
textarea.value = upperCase(textarea.value)
}
return false
}
function sentenceCase(str) {
var str = str.toLowerCase().replace(/\si\s/g, ' I ');
str = str.charAt(0).toUpperCase() + str.slice(1);
return str
}
//reference: https://github.com/gouch/to-title-case
function toTitleCase(str) {
var smallWords = /^(a|an|and|as|at|but|by|en|for|if|in|nor|of|on|or|per|the|to|vs?\.?|via)$/i;
var str = str.toLowerCase()
return str.replace(/[A-Za-z0-9\u00C0-\u00FF]+[^\s-]*/g, function(match, index, title) {
if (index > 0 && index + match.length !== title.length &&
match.search(smallWords) > -1 && title.charAt(index - 2) !== ":" &&
(title.charAt(index + match.length) !== '-' || title.charAt(index - 1) === '-') &&
title.charAt(index - 1).search(/[^\s-]/) < 0) {
return match.toLowerCase();
}
if (match.substr(1).search(/[A-Z]|\../) > -1) {
return match;
}
return match.charAt(0).toUpperCase() + match.substr(1);
});
};
//reference: https://medium.freecodecamp.com/three-ways-to-title-case-a-sentence-in-javascript-676a9175eb27
function CapitalCase(str) {
return str.toLowerCase().split(' ').map(function(word) {
return (word.charAt(0).toUpperCase() + word.slice(1));
}).join(' ');
}
function lowerCase(str) {
return str.toLowerCase();
}
function upperCase(str) {
return str.toUpperCase();
}
function wordandcharcount() {
wordcountspan.innerHTML = stringbox.value.split(' ').length
charcountspan.innerHTML = stringbox.value.length
}
stringbox.addEventListener('input', function() {
wordandcharcount()
}, false)
wordandcharcount()<textarea id="stringbox"></textarea>
<span id="wordcount"></span>
<span id="charcount"></span>
发布于 2022-08-14 19:32:19
我假设一个句子总是从输入字符串的开头开始,或者在句点之后立即开始。
var text = "this is a test. this is a test. this is a test.";
text = text.replace(/(^|\.\s*)([a-z])/g, function(match,c1,c2,offset,str) {
return c1 + c2.toUpperCase();
});
console.log(text)这将输出This is a test. This is a test. This is a test.
发布于 2022-08-14 19:48:26
这是一种方法。
const sentence = "this is talha. i am 8. i am a boy. I like to play soccer";
// convert to an array
const arr = sentence.split(".");
// capitalize first word for each sentence
let capitalized = arr.map((str, index) => {
if (index === arr.length - 1) {
str = str.trim().charAt(0).toUpperCase() + str.trim().slice(1);
return (str += " ");
}
str = str.trim().charAt(0).toUpperCase() + str.trim().slice(1) + ".";
return (str += " ");
});
// convert back to string and log
console.log(capitalized.join(""));https://stackoverflow.com/questions/73354459
复制相似问题