将文本截短到少于280个字符,然后将每个不包括“,”的单词截短到小于20个字符

问题描述 投票:1回答:1

这是我要更改的js

#tag1 #tag2 #thisisareallyreallyrealyylongtag3

进入

tag1, tag2, thisisareallyreallyrealyylongtag3

并且它还将结果截断为13个单词。

  var str9 = document.getElementById("hashtagsplain").innerHTML;
  var resu9 = str9.replace(/^#|( #)/g, (_, m1) => m1 ? ", " : '');  
  var truncatethen = resu9.split(" ").splice(0,13).join(" ").slice(0, -1);
document.getElementById("x").innerHTML = truncatethen;

我现在需要添加的内容是将整个文本截断为少于280个字符,然后将每个单词(标记)的末尾(不包括“,”的截断为少于20个字符。

我该添加些什么,以及发生在什么地方?

UPDATE ..

我已经添加了280个字符的限制部分

(/^(.{280}[^\s]*).*/, "$1")

现在我的代码看起来像这样。.

  var str9 = document.getElementById("hashtagsplain").innerHTML;
  var resu9 = str9.replace(/^#|( #)/g, (_, m1) => m1 ? ", " : '');  
  var resu10 = resu9.replace(/^(.{280}[^\s]*).*/, "$1");    
  var truncatethen = resu10.split(" ").splice(0,13).join(" ").slice(0, -1);
document.getElementById("x").innerHTML = truncatethen;

我现在需要弄清楚如何将不包括“,”在内的每个单词截断为少于20个字符,因此>

notalldisabilitiesarevisible, notalldisabilitiesarevisable, notalldisabilities

成为

notalldisabilitiesar, notalldisabilitiesar, notalldisabilities
<<

[sliceslice可以简化过程

javascript html truncate
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.