缩短Javascript功能

Spe*_*rds 11 javascript

我自己编写了一个函数来将字符串转换为缩写,它目前相当长,并且区分大小写.

我需要一种缩短它的方法,因此它可以100%的时间工作.目前,如果其中一个分词具有大写字母,如果一个单词以分词结尾,则会搞砸.

我的分词基本上就是我要删除的词(因为大多数公司都不包括它们).他们包括:

  • 对于

另外,我删除它们的方式是使用split和join(str.split('and ').join(''))这对我来说似乎不是最简单的方法.

除了这些问题,它工作正常.任何人都可以帮我缩小功能并解决问题吗?谢谢.

功能:

String.prototype.toAbbrev = function () {
    var s = [];
    var a = this.split('and ').join('').split('of ').join('').split('the').join('').split('for ').join('').split('to ').join('').split(' ');
    for (var i = 1; i < a.length + 1; i++) {
        s.push(a[i - 1].charAt(0).toUpperCase());
    }

    return s.join('.');
}
Run Code Online (Sandbox Code Playgroud)

经测试公司的产出

The National Aeronautics and Space Administration           ->    N.A.S.A
The National Roads and Motorists' Association               ->    N.R.M.A
Royal Society for the Prevention of Cruelty to Animals      ->    R.S.P.C.A

nde*_*ore 12

我认为这样的方法可能会更好:

var toAbbrev = function(str){
    return str.replace(/\b(?:and|of|the|for|to)(?: |$)/gi,''). // remove all occurances of ignored words
               split(' ').                                     // split into words by spaces
               map(function(x){                          
                   return x.charAt(0).toUpperCase();           // change each word into its first letter capitalized
               }).
               join('.');                                      // join with periods
};
Run Code Online (Sandbox Code Playgroud)

这是正则表达式的细分:

/
    \b                    // word boundary
    (?:and|of|the|for|to) // non-capturing group. matches and/of/the/for/to
    (?: |$)               // non-capturing group. matches space or end of string
/gi                       // flags: g = global (match all), i = case-insensitive
Run Code Online (Sandbox Code Playgroud)

这是一个具有不太复杂的正则表达式的替代方法:

var toAbbrev = function(str){
    return str.split(' '). // split into words
               filter(function(x){
                   return !/^(?:and|of|the|for|to)$/i.test(x); // filter out excluded words
               }).
               map(function(x){
                    return x.charAt(0).toUpperCase(); // convert to first letter, captialized
               }).
               join('.'); // join with periods
};
Run Code Online (Sandbox Code Playgroud)

和正则表达式分解:

/
    ^                     // start of string
    (?:and|of|the|for|to) // non-capturing group. matches and/of/the/for/to
    $                     // end of string
/i                        // flags: i = case-insensitive
Run Code Online (Sandbox Code Playgroud)


Der*_*會功夫 8

更短的一个:

str.replace(/(and|of|the|for|to)( |$)/gi, "").replace(/(.).+?(\s|$)/g, "$1.");
Run Code Online (Sandbox Code Playgroud)

为了确保它是大写的,你可以.toUpperCase在最后做.

(.)     //selects the first character
.+      //matches the rest of the characters
  ?     //? indicates a lazy match
(\s|$)  //match a space or the end

$1.     //means "the first selected match plus a dot"
Run Code Online (Sandbox Code Playgroud)

让它成为一个正则表达式!

str.replace(/((and|of|the|for|to) )*(.).+?(\s|$)/ig, "$3.");
Run Code Online (Sandbox Code Playgroud)
"Royal Society for the Prevention of Cruelty to Animals"
    .replace(/((and|of|the|for|to) )*(.).+?(\s|$)/ig, "$3.");
//R.S.P.C.A

"Josie and the Pussycats"
    .replace(/((and|of|the|for|to) )*(.).+?(\s|$)/ig, "$3.");
//J.P.
Run Code Online (Sandbox Code Playgroud)

从理论上讲,这应涵盖所有合法的名称.对于末尾有介词的名字,你可以从技术上做到这一点:

.replace(/((and|of|the|for|to) )*(.).+?(\s|$)((and|of|the|for|to) ?)*/ig, "$3.")
Run Code Online (Sandbox Code Playgroud)

但这明显比两个人更长replace,这就失去了它的目的.