亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何顯示所有結果而不以第一場比賽結束?

如何顯示所有結果而不以第一場比賽結束?

小唯快跑啊 2023-08-05 19:33:21
如何顯示所有結果而不以第一場比賽結束?此代碼幫助我獲取開始詞和結束詞之間的文本。但搜索在找到第一對后就結束了。您如何找到所有匹配項?const file = fs.readFileSync('./history.txt', 'utf8')const startString = '-----CompilerOutput:-stderr----------'const endString = '-----EndCompilerOutput---------------'const startIndex = file.indexOf(startString) + startString.lengthconst endIndex = file.indexOf(endString)const between = file.slice(startIndex, endIndex)console.log(between)
查看完整描述

1 回答

?
函數式編程

TA貢獻1807條經驗 獲得超9個贊

我希望我理解正確


const file = fs.readFileSync('./history.txt', 'utf8');


const startString = '-----CompilerOutput:-stderr----------';

const endString = '-----EndCompilerOutput---------------';

var startIndex = file.indexOf(startString);


while(startIndex > -1) {

    // get index after '-----CompilerOutput:-stderr----------'

    let start = startIndex + startString.length;


// -----------------------------------

// EDIT: WRONG --> let end = file.indexOf(endString);

// Need to start searching for next occurence starting at index after the start

// -----------------------------------


    // get index before '-----EndCompilerOutput---------------'

    let end = file.indexOf(endString, start);


    // get the text between

    console.log(file.slice(start, end));


    // set startIndex to next index of '-----CompilerOutput:-stderr----------'

    // if not found the startIndex value will be -1 so end of the while loop

    startIndex = file.indexOf(startString, end + endString.length);

}

編輯:


示例文件:


-----CompilerOutput:-stderr----------

test1

-----EndCompilerOutput---------------

-----CompilerOutput:-stderr----------

test2

-----EndCompilerOutput---------------

輸出:


test1



test2


查看完整回答
反對 回復 2023-08-05
  • 1 回答
  • 0 關注
  • 122 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號