首页 > 解决方案 > True Sentence Case(处理常见句子)

问题描述

真句案例

这不应与“标题案例”相混淆。句格只大写句子的第一个单词;这句话的其他词保持不变。

Stackoverflow 上的各种文章和答案都没有解决下面解释的问题:最相关的(虽然 6 岁)是在 javascript 中将字符串转换为句子大小写,但不起作用。

一个段落(或文章)可能包括几个由句号“.”分隔的句子。

我正在寻找至少应该处理下面描述的情况的“SentenceCase”代码。

目的是仅将第一个单词(如果适用)大写,而不是尝试修改或尝试更正句子文本的其余部分。

该功能应解决以下标准情况:

// 1. Simple sentence (no full-stop):
// ----------------------------------

// Original:  this is a sentence
// Becomes :  This is a sentence
// 2. Two or more sentences:
// -------------------------

// Original:  first sentence. second sentence .   third sentence.
// Becomes :  First sentence. Second sentence .   Third sentence.
// 3. Sentences starting with a single char:
// -----------------------------------------

// Original:  a world of fun. a world of fun.
// Becomes :  A world of fun. A world of fun.
// 4. Sentences starting with a word that has an in-between cap:
// -------------------------------------------------------------

// Original:  kHz is a unit of measure. kPascal too.
// Becomes :  kHz is a unit of measure. kPascal too.

// Original:  iPads are sold by Apple. iTones are too.
// Becomes :  iPads are sold by Apple. iTones are too.
// 5. Sentences starting with a special chars/words:
// -------------------------------------------------

// Original:  https//: is the web address. www.stack.com is another website.
// Becomes :  https//: is the web address. www.stack.com is another website.

// Original:  .Net is a programming language. foo_bar is a variable it uses.
// Becomes :  .Net is a programming language. foo_bar is a variable it uses.

// Original:  string_length is a snake case type variable.
// Becomes :  string_length is a snake case type variable.

// Original:  abc@domain.net is my email.   defgh@dmain.net is his.
// Becomes :  abc@domain.net is my email.   defgh@dmain.net is his.

// Original:  concrete5 is an open-source content management system.
// Becomes :  concrete5 is an open-source content management system.
// 6. Text inside quotes:
// ----------------------

// Original:  "we should stay home" said the governor. 'we will' we answered.
// Becomes :  "we should stay home" said the governor. 'we will' we answered.

// Original:  "this ". was text in quotes
// Becomes :  "this ". Was text in quotes
// 7. Sentences with Abbreviations:
// --------------------------------

// Original:  reporting time is 09:00 a.m. and leaving at 05:00 p.m. every day.
// Becomes :  Reporting time is 09:00 a.m. and leaving at 05:00 p.m. every day.
// 8. Short sentences with single words:
// -------------------------------------

// Original:   organization
// Becomes :   Organization

// Original:   development.
// Becomes :   Development.

标签: javascriptstring

解决方案


推荐阅读