Reputation: 41
I need to filter the category type only but instead my code is returning the defined string.
My expected output is: funiture sofa galley.
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;
}
Upvotes: 0
Views: 56
Reputation: 1625
can you give this a try ??
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 pos = 0;
var printcat = '';
while (pos = contextstr.indexOf('category:')) {
contextstr = contextstr.substring(pos+9);
let cat = contextstr.substring(0, contextstr.indexOf(' '));
contextstr = contextstr.substring(cat.length+1);
printcat += cat + "<br>";
}
document.write(printcat);
Upvotes: 0
Reputation: 15685
change this line:
var printcat = contextstr.substring(startpos + endpos);
to:
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;
}
Upvotes: 1
Reputation: 14098
Try this:
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
Upvotes: 1
Reputation: 1440
Try this:
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>');
});
Upvotes: 2