为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

智慧大石 2022-05-26 13:59:09
我只需要过滤类别类型,而是我的代码返回定义的字符串。我的预期输出是:家具沙发厨房。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;}
查看完整描述

3 回答

?
精慕HU

TA贡献1845条经验 获得超8个赞

试试这个:


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>');

});


查看完整回答
反对 回复 2022-05-26
?
偶然的你

TA贡献1841条经验 获得超3个赞

改变这一行:


var printcat = contextstr.substring(startpos + endpos);

到:


var printcat = contextstr.substring(startpos,endpos);

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 x = 1;

var count = 0;

while (x=1) {

  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;

}


查看完整回答
反对 回复 2022-05-26
?
元芳怎么了

TA贡献1798条经验 获得超7个赞

试试这个:


const 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';


const matches = [...contextstr.matchAll(/(?<=category:)\S+/g)];


document.write(`${matches.join('\n')}<br>`);


的解释/(?<=category:)\S+/g

/

  (?<=         positive lookbehind

    category:  matches anything with category: before it

  )            end of positive lookbehind

  \S           matches anything that's not a whitespace character

    +          ...if there are one or more of them

/g             global flag: match multiple times


查看完整回答
反对 回复 2022-05-26
  • 3 回答
  • 0 关注
  • 101 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信