简体   繁体   English

如何找到所有匹配项?

[英]How to find all the matches?

This code helps me find text between start&end words.此代码帮助我在开始和结束单词之间查找文本。 But the search ends after the first pair found.但是搜索在找到第一对之后结束。 How to find all the matches?如何找到所有匹配项?

const file = fs.readFileSync('./history.txt', 'utf8')
const startString = '-----CompilerOutput:-stderr----------'
const endString = '-----EndCompilerOutput---------------'
const startIndex = file.indexOf(startString) + startString.length
const endIndex = file.indexOf(endString)
const between = file.slice(startIndex, endIndex)
console.log(between)

Use

const startIndex = file.match(/-----CompilerOutput:-stderr----------/gi)
const endIndex = file.match(/-----EndCompilerOutput---------------/gi)

Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions参考: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions

https://www.w3schools.com/jsref/jsref_match.asp https://www.w3schools.com/jsref/jsref_match.asp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM