首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >句子案例工具-并非所有句子都以大写字母开头

句子案例工具-并非所有句子都以大写字母开头
EN

Stack Overflow用户
提问于 2022-08-14 19:13:44
回答 2查看 71关注 0票数 0

你能看看这段代码吗。我需要解决这个案子的选项。每个句子的第一个字母应该大写。

这是测试。这是测试。这是测试。

问题是,只有第一句以大写字母开头,而其他句子则没有。

https://github.com/freewarelovers/CaseConverter

代码语言:javascript
复制
// 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()
代码语言:javascript
复制
<textarea id="stringbox"></textarea>
<span id="wordcount"></span>
<span id="charcount"></span>

EN

回答 2

Stack Overflow用户

发布于 2022-08-14 19:32:19

我假设一个句子总是从输入字符串的开头开始,或者在句点之后立即开始。

代码语言:javascript
复制
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.

票数 2
EN

Stack Overflow用户

发布于 2022-08-14 19:48:26

这是一种方法。

代码语言:javascript
复制
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(""));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73354459

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档