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

Get the first letter of each word preceded by space or dot

I need to take the first letter of each word in a string when preceded by space or .

Example 1: Hello World I'm a web.dev from.japan.com cool

Needed solution 1 : HWIAW.DF.J.CC

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

Example 2: Hello World This is awesome

Needed solution 2 : HWTIA

EDIT : (I wasn’t very clear)

All I could came up with is this :

const sentence = "Hello World I'm a web.dev from.japan.com cool";
sentence.split(/[\s.]+/).map((n) => n[0]).join("").toUpperCase();

Gives me the result : HWIAWDFJCC, but I need to maintain the dots too.

But this doesn’t solve my case.

>Solution :

You can combine those 2 actions in one chained method – first removing extra spaces, then splitting by space, and finally splitting by period.

let ex1="Hello World I'm a web.dev from.japan.com cool"
//Needed solution 1 : HWIAW.DF.J.CC

let ex2= "Hello World This   is awesome";
//Needed solution 2 : HWTIA

const trans = (str) => str.replace(/  +/g, ' ').split(' ').map(s => s.split('.').map(t => t[0]).join('.')).join('').toUpperCase() ;
console.log(trans(ex1));
console.log(trans(ex2));
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