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