此刻,我將一段文本傳遞給以下函數,以確保每個句子的首字母都大寫。句子案例忽略了html元素中的文本段落
function sentenceCase(string) {
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);
return vfinal;
}
如果文本不包含任何元素,並且所有內容都應該是大寫的,這很有效。
var str1 = 'he always has a positive contribution to make to the class. in class, he behaves well, but he should aim to complete his homework a little more regularly.';
console.log(sentenceCase(str1));
Returns >>> He always has a positive contribution to make to the class. In class, he behaves well, but he should aim to complete his homework a little more regularly.
但是,如果文本包含<span>
元素在句子中包裹的第一個字,那麼它顯然會導致問題,如圖所示。
var str2 = '<span class="pronoun subjective">he</span> always has a positive contribution to make to the class. in class, <span class="pronoun subjective">he</span> behaves well, but <span class="pronoun subjective">he</span> should aim to complete <span class="pronoun possessive">his</span> homework a little more regularly.';
console.log(sentenceCase(str2));
Returns >>> <span class="pronoun subjective">he</span> always has a positive contribution to make to the class. In class, <span class="pronoun subjective">he</span> behaves well, but <span class="pronoun subjective">he</span> should aim to complete <span class="pronoun possessive">his</span> homework a little more regularly.
我正則表達式的技巧是遠遠恆星,所以我不知道如何從這裏出發,所以如何將其轉換爲句首字母大寫時忽略任何元素文本中的任何建議,將不勝感激。
編輯:爲了澄清 - 輸出應該仍然保持元素 - 他們只需要在考慮上層的句子時忽略。
你的輸出應該包含HTML元素,或者它應該被刪除像你正在尋找他 ...或者只是他......... –
可以消毒該字符串在傳遞給函數之前: 'string = string.replace(/ <.*?>/g,'');'這將刪除HTML標籤。 – sideroxylon
對不起 - 澄清 - 輸出仍應保持元素 - 當考慮上層套管的句子時,他們只需要被忽略。 – user6790086