我希望輸入的字符串在任何情況下都應轉換爲句子案例。在javascript中將字符串轉換爲句子案例
像
大家好,這是DERP。謝謝大家回答我的問題。
轉換爲
大家好,這是DERP。謝謝大家回答我的問題。
我希望輸入的字符串在任何情況下都應轉換爲句子案例。在javascript中將字符串轉換爲句子案例
像
大家好,這是DERP。謝謝大家回答我的問題。
轉換爲
大家好,這是DERP。謝謝大家回答我的問題。
試試這個,它對你來說工作的很好。它也可以用於有領先空間的字符串。
var string="hi all, this is derp. thank you all to answer my query.";
var n=string.split(".");
var vfinal=""
for(i=0;i<n.length;i++)
{
var spaceput=""
var spaceCount=n[i].replace(/^(\s*).*$/,"$1").length;
n[i]=n[i].replace(/^\s+/,"");
var newstring=n[i].charAt(n[i]).toUpperCase() + n[i].slice(1);
for(j=0;j<spaceCount;j++)
spaceput=spaceput+" ";
vfinal=vfinal+spaceput+newstring+".";
}
vfinal=vfinal.substring(0, vfinal.length - 1);
alert(vfinal);
我想出了這樣的正則表達式的:
var rg = /(^\w{1}|\.\s*\w{1})/gi;
var myString = "hi all, this is derp. thank you all to answer my query.";
myString = myString.replace(rg, function(toReplace) {
return toReplace.toUpperCase();
});
它不適用於「。」後有空格的字符串。 – unknownbits
好吧,我會在那裏添加一個星號,它會。 – zvona
good.it很短。我喜歡這個答案。 – unknownbits
試用演示
http://jsfiddle.net/devmgs/6hrv2/
function sentenceCase(strval){
var newstrs = strval.split(".");
var finalstr="";
//alert(strval);
for(var i=0;i<newstrs.length;i++)
finalstr=finalstr+"."+ newstrs[i].substr(0,2).toUpperCase()+newstrs[i].substr(2);
return finalstr.substr(1);
}
當心所有點並不總是代表行的結束,可以是縮寫等,也不知道是否在完全停止後輸入空格。這些情況使這個腳本變得脆弱。
你也可以試試這個
<script>
var name="hi all, this is derp. thank you all to answer my query.";
var n = name.split(".");
var newname="";
for(var i=0;i<n.length;i++)
{
var j=0;
while(j<n[i].length)
{
if(n[i].charAt(j)!= " ")
{
n[i] = n[i].replace(n[i].charAt(j),n[i].charAt(j).toUpperCase());
break;
}
else
j++;
}
newname = newname.concat(n[i]+".");
}
alert(newname);
</script>
這是我最終使用的解決方案:
str = 'hi all, this is derp. thank you all to answer my query.';
temp_arr = str.split('.');
for (i = 0; i < temp_arr.length; i++) {
temp_arr[i]=temp_arr[i].trim()
temp_arr[i] = temp_arr[i].charAt(0).toUpperCase() + temp_arr[i].substr(1).toLowerCase();
}
str=temp_arr.join('. ') + '.';
return str;
下面的代碼爲我工作正常。
function toSentenceCase(inputString) {
inputString = "." + inputString;
var result = "";
if (inputString.length == 0) {
return result;
}
var terminalCharacterEncountered = false;
var terminalCharacters = [".", "?", "!"];
for (var i = 0; i < inputString.length; i++) {
var currentChar = inputString.charAt(i);
if (terminalCharacterEncountered) {
if (currentChar == ' ') {
result = result + currentChar;
} else {
var currentCharToUpperCase = currentChar.toUpperCase();
result = result + currentCharToUpperCase;
terminalCharacterEncountered = false;
}
} else {
var currentCharToLowerCase = currentChar.toLowerCase();
result = result + currentCharToLowerCase;
}
for (var j = 0; j < terminalCharacters.length; j++) {
if (currentChar == terminalCharacters[j]) {
terminalCharacterEncountered = true;
break;
}
}
}
result = result.substring(1, result.length - 1);
return result;
}
我寫了一個基於FSM的函數來合併多個空格字符並將字符串轉換爲句子。它應該很快,因爲它不使用複雜的正則表達式或split
並假設您的JavaScript運行時具有有效的字符串連接,那麼這應該是最快的方式。它還可以讓您輕鬆添加特殊情況例外。
通過用函數替換空格正則表達式可以進一步提高性能,以便比較字符代碼。
function toSentenceCase(str) {
var states = {
EndOfSentence : 0,
EndOfSentenceWS: 1, // in whitespace immediately after end-of-sentence
Whitespace : 2,
Word : 3
};
var state = states.EndOfSentence;
var start = 0;
var end = 0;
var output = "";
var word = "";
function specialCaseWords(word) {
if(word == "i") return "I";
if(word == "assy") return "assembly";
if(word == "Assy") return "Assembly";
return word;
}
for(var i = 0; i < str.length; i++) {
var c = str.charAt(i);
switch(state) {
case states.EndOfSentence:
if(/\s/.test(c)) { // if char is whitespace
output += " "; // append a single space character
state = states.EndOfSentenceWS;
}
else {
word += c.toLocaleUpperCase();
state = states.Word;
}
break;
case states.EndOfSentenceWS:
if(!(/\s/.test(c))) { // if char is NOT whitespace
word += c.toLocaleUpperCase();
state = states.Word;
}
break;
case states.Whitespace:
if(!(/\s/.test(c))) { // if char is NOT whitespace
output += " "; // add a single whitespace character at the end of the current whitespace region only if there is non-whitespace text after.
word += c.toLocaleLowerCase();
state = states.Word;
}
break;
case states.Word:
if(c == ".") {
word = specialCaseWords(word);
output += word;
output += c;
word = "";
state = states.EndOfSentence;
} else if(!(/\s/.test(c))) { // if char is NOT whitespace
// TODO: See if `c` is punctuation, and if so, call specialCaseWords(word) and then add the puncutation
word += c.toLocaleLowerCase();
}
else {
// char IS whitespace (e.g. at-end-of-word):
// look at the word we just reconstituted and see if it needs any special rules
word = specialCaseWords(word);
output += word;
word = "";
state = states.Whitespace;
}
break;
}//switch
}//for
output += word;
return output;
}
...和?你有什麼嘗試?你卡在哪裏? –
提示:嘗試編寫正則表達式。找到句子的第一個字母,並使用句號+空格分隔句子。祝你好運。如果嘗試正則表達式後仍然需要幫助,請將其發佈到此處,然後我們會看到我們可以做些什麼。 – Jackson
爲什麼你可以用CSS來做javascript? –