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

我只需要过滤类别类型,而是我的代码返回定义的字符串。

我的预期输出是:家具沙发厨房。


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;

}


智慧大石
浏览 102回答 3
3回答

精慕HU

试试这个:var contextstr =&nbsp; '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 => {&nbsp; document.write(split.split(" ")[0] + '<br>');});

偶然的你

改变这一行:var printcat = contextstr.substring(startpos + endpos);到:var printcat = contextstr.substring(startpos,endpos);var contextstr =&nbsp; &nbsp;&nbsp;'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 x = 1;var count = 0;while (x=1) {&nbsp; var findpos = contextstr.indexOf('category:', count);&nbsp; if (findpos == -1) break;&nbsp;&nbsp;&nbsp; var startpos = findpos + 9;&nbsp; var endpos = contextstr.indexOf(' ', startpos);&nbsp;&nbsp;&nbsp; var printcat = contextstr.substring(startpos, endpos);&nbsp;&nbsp;&nbsp;document.write(printcat + '<br>');&nbsp; //x++;&nbsp; count = endpos + 1;}

元芳怎么了

试试这个:const contextstr =&nbsp; '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';const matches = [...contextstr.matchAll(/(?<=category:)\S+/g)];document.write(`${matches.join('\n')}<br>`);的解释/(?<=category:)\S+/g/&nbsp; (?<=&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;positive lookbehind&nbsp; &nbsp; category:&nbsp; matches anything with category: before it&nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end of positive lookbehind&nbsp; \S&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;matches anything that's not a whitespace character&nbsp; &nbsp; +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ...if there are one or more of them/g&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;global flag: match multiple times
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript