3 回答
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>');
});
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;
}
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
添加回答
举报