如何在JavaScript中将每个句子的第一个字母大写?

huangapple go评论54阅读模式
英文:

How do i capitalize the first letter of each sentance in Javascript?

问题

如何在JavaScript中将每个句子的第一个字母大写?

(我正在创建一个工具,在ReactJS中点击后将每个句子的第一个字母转换为大写(句子格式))

我想要的输出如下:

如何在JavaScript中将每个句子的第一个字母大写?如何在JavaScript中将每个句子的第一个字母大写?如何在JavaScript中将每个句子的第一个字母大写?如何在JavaScript中将每个句子的第一个字母大写?

我正在尝试以下代码,但似乎不正确:

let newText = text.charAt(0).toUpperCase() + text.slice(1).toLowerCase();

let result = newText.map((val) => val);

setText(result);
英文:

How do i capitalize the first letter of each sentance in Javascript?

(I am creating a tool that would convert first letter of each sentance to upparcase(Sentance Case) on click in reactjs)

I want output like this,

How do i capitalize the first letter of each sentance in Javascript? How do i capitalize the first letter of each sentance in Javascript? How do i capitalize the first letter of each sentance in Javascript? How do i capitalize the first letter of each sentance in Javascript?

I am trying this but looks wrong,

let newText = text.charAt(0).toUpperCase() + text.slice(1).toLowerCase();

let result = newText.map((val) => val);

setText(result);

答案1

得分: 1

function capitalizeSentences(text) {
  return text.replace(/(^\w|\.\s*\w)/g, function(match) {
    return match.toUpperCase();
  });
}
var text = "this is how you would captilize this sentence. this is another sentence"
var capitalizedText = capitalizeSentences(text);

console.log(capitalizedText)

// >>> This is how you would captilize this sentence. This is another sentence
The replace method is used with a regular expression pattern to match the first word character after the start of the string (^\w) or after a period followed by zero or more whitespace characters (\.\s*\w).
英文:
function capitalizeSentences(text) {
  return text.replace(/(^\w|\.\s*\w)/g, function(match) {
    return match.toUpperCase();
  });
}
var text = "this is how you would captilize this sentence. this is another sentence"
var capitalizedText = capitalizeSentences(text);

console.log(capitalizedText)

// >>> This is how you would captilize this sentence. This is another sentence

The replace method is used with a regular expression pattern to match the first word character after the start of the string (^\w) or after a period followed by zero or more whitespace characters (.\s*\w).

答案2

得分: 0

const text = 'lorem ipsum dolor sit amet consectetur adipisicing elit. "voluptas, debitis?" unde, delectus pariatur, velit vero "dolorem repellendus" veritatis. quia odio... aperiam (nemo) sint natus 0.1 hic ad nisi id magni praesentium.';

const result = text.replace(/(?<=(?:^|[.?!])\W*)[a-z]/g, i => i.toUpperCase());

console.log(result);

英文:
const text = &#39;lorem ipsum dolor sit amet consectetur adipisicing elit. &quot;voluptas, debitis?&quot; unde, delectus pariatur, velit vero &quot;dolorem repellendus&quot; veritatis. quia odio... aperiam (nemo) sint natus 0.1 hic ad nisi id magni praesentium.&#39;;

const result = text.replace(/(?&lt;=(?:^|[.?!])\W*)[a-z]/g, i =&gt; i.toUpperCase());

console.log(result);

huangapple
  • 本文由 发表于 2023年5月29日 18:25:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76356530.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定