Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 creating a tool that would convert first letter of each sentance to upparcase(Sentance Case) on click in reactjs)

I want output like this,

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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);

>Solution :

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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading