gpt4 book ai didi

javascript - 将文本 chop 为少于 280 个字符,然后将除 ","之外的每个单词少于 20 个字符

转载 作者:行者123 更新时间:2023-11-28 03:14:21 24 4
gpt4 key购买 nike

这是我要改变的js

#tag1 #tag2 #thisisareallyreallyrealyylongtag3

进入

tag1, tag2, thisisareallyreallyrealyylongtag3

它还将结果 chop 为 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;

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

我需要添加什么以及在哪里才能发生这种情况?

更新..

我通过添加解决了 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;

我现在需要弄清楚如何将除“,”之外的每个单词 chop 为少于 20 个字符

notalldisabilitiesarevisible, notalldisabilitiesarevisable, notalldisabilities

变成了

notalldisabilitiesar, notalldisabilitiesar, notalldisabilities

最佳答案

slicemap可以简化流程

var str9 = '#tag1 #tag2 #thisisareallyreallyrealyylongtag3 #notalldisabilitiesarevisible #notalldisabilitiesar #notalldisabilitiesar #notalldisabilities #tag1 #tag2 #thisisareallyreallyrealyylongtag3 #notalldisabilitiesarevisible #notalldisabilitiesar #notalldisabilitiesar #notalldisabilities #tag1 #tag2 #thisisareallyreallyrealyylongtag3 #notalldisabilitiesarevisible #notalldisabilitiesar #notalldisabilitiesar #notalldisabilities'

const textLength = 280;
const wordLength = 20;

var resu9 = str9.slice(1, textLength+1).replace(/#/g, ',');
var truncatethen = resu9.split(',').map(str => str.slice(0, wordLength));
truncatethen.forEach(str => console.log(str))

关于javascript - 将文本 chop 为少于 280 个字符,然后将除 ","之外的每个单词少于 20 个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59774437/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com