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

后端 未结 17 1632
后悔当初
后悔当初 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:29

    This is made very simple with ES6

    string.split(' ').map(i => i.charAt(0))               //Inherit case of each letter
    string.split(' ').map(i => i.charAt(0)).toUpperCase() //Uppercase each letter
    string.split(' ').map(i => i.charAt(0)).toLowerCase() //lowercase each letter
    

    This ONLY works with spaces or whatever is defined in the .split(' ') method

    ie, .split(', ') .split('; '), etc..

    0 讨论(0)
  • 2020-12-05 04:30

    @BotNet flaw: i think i solved it after excruciating 3 days of regular expressions tutorials:

    ==> I'm a an animal

    (used to catch m of I'm) because of the word boundary, it seems to work for me that way.

    /(\s|^)([a-z])/gi
    
    0 讨论(0)
  • 2020-12-05 04:30

    This is similar to others, but (IMHO) a tad easier to read:

    const getAcronym = title =>
      title.split(' ')
        .map(word => word[0])
        .join('');
    
    0 讨论(0)
  • 2020-12-05 04:32

    Try -

    var text = '';
    var arr = "Java Script Object Notation".split(' ');
    for(i=0;i<arr.length;i++) {
        text += arr[i].substr(0,1)
    }    
    alert(text);
    

    Demo - http://jsfiddle.net/r2maQ/

    0 讨论(0)
  • 2020-12-05 04:35

    How about this:

    var str = "", abbr = "";
    str = "Java Script Object Notation";
    str = str.split(' ');
    for (i = 0; i < str.length; i++) {
        abbr += str[i].substr(0,1);
    }
    alert(abbr);
    

    Working Example.

    0 讨论(0)
  • 2020-12-05 04:39

    Alternative 1:

    you can also use this regex to return an array of the first letter of every word

    /(?<=(\s|^))[a-z]/gi
    

    (?<=(\s|^)) is called positive lookbehind which make sure the element in our search pattern is preceded by (\s|^).


    so, for your case:

    // in case the input is lowercase & there's a word with apostrophe
    
    const toAbbr = (str) => {
      return str.match(/(?<=(\s|^))[a-z]/gi)
                .join('')
                .toUpperCase();
    };
    
    toAbbr("java script object notation"); //result JSON
    

    (by the way, there are also negative lookbehind, positive lookahead, negative lookahead, if you want to learn more)


    Alternative 2:

    match all the words and use replace() method to replace them with the first letter of each word and ignore the space (the method will not mutate your original string)

    // in case the input is lowercase & there's a word with apostrophe    
    
    const toAbbr = (str) => {
      return str.replace(/(\S+)(\s*)/gi, (match, p1, p2) => p1[0].toUpperCase());
    };
    
    toAbbr("java script object notation"); //result JSON
    
    // word = not space = \S+ = p1 (p1 is the first pattern)
    // space = \s* = p2 (p2 is the second pattern)
    
    0 讨论(0)
提交回复
热议问题