JavaScript拆分和加入

olo*_*olo 1 javascript jquery

我有一个字符串,15.Prototypal-Inheritance-and-Refactoring-the-Slider.txt我想让它看起来像15.Prototypal...-Slider.txt

length文的是56,我怎么能保持第一12个字母和10个最后一个字母(incuding标点符号),并更换他人...

我真的不知道如何开始代码,我做了类似的事情

var str="15.Prototypal-Inheritance-and-Refactoring-the-Slider.txt";
str.split("// ",1);
Run Code Online (Sandbox Code Playgroud)

虽然这给了我我需要的东西,但我如何得到基于字母而不是单词的结果.

mqu*_*lle 6

您可以使用str.slice().

function middleEllipsis(str, a, b) {
  if (str.length > a + b)
    return str.slice(0, a) + '...' + str.slice(-b);
  else
    return str;
}

middleEllipsis("15.Prototypal-Inheritance-and-Refactoring-the-Slider.txt", 12, 10);
// "15.Prototypa...Slider.txt"

middleEllipsis("mpchc64.mov", 12, 10);
// "mpchc64.mov"
Run Code Online (Sandbox Code Playgroud)