gpt4 book ai didi

javascript - 通过迭代,我需要使用 javascript 过滤特定单词,但我没有得到预期值?

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

我只需要过滤类别类型,但我的代码返回定义的字符串。
我的预期输出是:家具沙发厨房。

var contextstr =
'Lorem Ipsum is category:funiture dummy text of the printing and typesetting industry. Lorem Ipsum has been the industrys standard dummy text ever since category:sofa 1500s, when an unknown printer took a category:galley of type and scrambled it to make a type';

var count = 0;
while (true) {
var findpos = contextstr.indexOf('category:', count);
if (findpos == -1) break;
var startpos = findpos + 9;
var endpos = contextstr.indexOf(' ', startpos);
var printcat = contextstr.substring(startpos + endpos);
document.write(printcat + '<br>');
//x++;
count = endpos + 1;
}

最佳答案

试试这个:

var contextstr =
'Lorem Ipsum is category:funiture dummy text of the printing and typesetting industry. Lorem Ipsum has been the industrys standard dummy text ever since category:sofa 1500s, when an unknown printer took a category:galley of type and scrambled it to make a type';

var splitArray = contextstr.split("category:");
splitArray.shift(); //Remove first item (Stuff right before first 'category:')
splitArray.forEach(split => {
document.write(split.split(" ")[0] + '<br>');
});

关于javascript - 通过迭代,我需要使用 javascript 过滤特定单词,但我没有得到预期值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60792793/

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