Get first letter of each word in a string, in JavaScript

I think what you’re looking for is the acronym of a supplied string.

var str = "Java Script Object Notation";
var matches = str.match(/\b(\w)/g); // ['J','S','O','N']
var acronym = matches.join(''); // JSON

console.log(acronym)

Note: this will fail for hyphenated/apostrophe’d words Help-me I'm Dieing will be HmImD. If that’s not what you want, the split on space, grab first letter approach might be what you want.

Here’s a quick example of that:

let str = "Java Script Object Notation";
let acronym = str.split(/\s/).reduce((response,word)=> response+=word.slice(0,1),'')

console.log(acronym);

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)