有一个函数可以很好地将字符串缩短为一个、两个或三个字母

There is a function to shorten nicely a string to one, two or three letters

有function/lib等很好的缩短一个字符串(类似ios圆圈联系但更进化)?

例如:

我真的可以自己写每个案例我 可以 看到,但我正在寻找一些不错的东西,也许更 聪明我的示例(省略文章超过 3 个单词,如果超过 3 个单词则省略破折号等)。

精度:nice 是我的问题。我正在寻找像 stringjs 这样的库。com/underscorejs(规范化、大写、slugify...)或一个简单的函数,但有一个很好的缩短方法。

目前,我使用这段代码(更新 w/terabaud 回答,thx dude):

function shortener(label) {
            //http://www.ranks.nl/stopwords
            var stopWords = "alors au aucuns aussi autre avant avec avoir bon car "
                + "ce cela ces ceux chaque ci comme comment dans des du "
                + "dedans dehors depuis deux devrait doit donc dos droite "
                + "début elle elles en encore essai est et eu fait faites "
                + "fois font force haut hors ici il ils je juste la le les "
                + "leur là ma maintenant mais mes mine moins mon mot même "
                + "ni nommés notre nous nouveaux ou où par parce parole "
                + "pas personnes peut peu pièce plupart pour pourquoi quand "
                + "que quel quelle quelles quels qui sa sans ses seulement "
                + "si sien son sont sous soyez sujet sur ta tandis tellement "
                + "tels tes ton tous tout trop très tu valeur voie voient "
                + "vont votre vous vu ça étaient état étions été être";

            var articles = stopWords.split(' ');

            return (label || "")
                .replace(/[A-Z]/g, " $&") // add space before each capital
                .replace(/[_\-']/g, " ") // replace _ - with spaces
                .split(" ")
                .filter(function (word) {
                    return word !== ""
                })
                .map(function (word, idx, arr) {
                    // return the first letter of each word
                    // if there are more than 2 words, omit articles
                    return (arr.length > 2 && articles.indexOf(word.toLowerCase()) > -1) ? "" : word[0];
                }).join("").slice(0, 3);
 
  };
    ["John Doe", "My board", "Something very long", "My Very long board", "Dash-board", "title", "Title", "JohnDoe","avec unwanted Words", "John_Doe"].forEach(function(str) {
      document.body.innerHTML += shortener(str) + '<br>';
    });

如何在存储 String 的变量上使用 indexOf 来查找:

space、大写字母或破折号的前三个实例。

这三个字母会给你一个很好的缩写。

我会按以下顺序使用正则表达式:

  1. 在任何大写字母前加上 space
  2. 将特殊字符更改为 space
  3. 删除每个单词中第一个字母之后的所有字母
  4. 全部删除space

没有简单的方法可以做到这一点。尝试这样的事情:

var articles = ["the", "a", "an"];

function shorten(str) {
  return (str || "")
    .replace(/[A-Z]/g, " $&") // add space before each capital
    .replace(/[_\-]/g, " ") // replace _ - with spaces
    .split(" ")
    .filter(function (word) { return word !== "" })
    .map(function(word, idx, arr) {
      // return the first letter of each word
      // if there are more than 3 words, omit articles
      return arr.length > 3 &&
        articles.indexOf(word.toLowerCase()) > -1 ?
        "" : word[0];
    }).join("").slice(0, 3);
}

["John Doe", "My board", 
 "Something very long", 
 "My Very long board",
 "Dash-board", 
 "title", 
 "Title", 
 "JohnDoe", 
 "John_Doe"].forEach(function(title) {
  document.body.innerHTML += shorten(title) + '<br>';
});

我不确定这是不是你想要的:请在下面评论

"My Very Long Board".split(' ').map(function(word){
 return word[0];
}).join('').slice(0, 3)