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

后端 未结 17 1661
后悔当初
后悔当初 2020-12-05 04:00

How would you go around to collect the first letter of each word in a string, as in to receive an abbreviation?

Input: "Java Script Object

17条回答
  •  清歌不尽
    2020-12-05 04:48

    I think you can do this with

    'Aa Bb'.match(/\b\w/g).join('')
    

    Explanation: Obtain all /g the alphanumeric characters \w that occur after a non-alphanumeric character (i.e: after a word boundary \b), put them on an array with .match() and join everything in a single string .join('')


    Depending on what you want to do you can also consider simply selecting all the uppercase characters:

    'JavaScript Object Notation'.match(/[A-Z]/g).join('')
    

提交回复
热议问题