0

I am reading in a file line by line in js and looking for any string within quotes, as follows:

// regex to find all strings within double-quotes
var re = new RegExp("\"([^\"]*)\"");

// parse each line in inFile
var lineReader = require('readline').createInterface({
    input: require('fs').createReadStream(inFile)
});

lineReader.on('line', function(line) {
    var res = line.match(re)
    // do something else here
});

This works great if there is only one string on each line within quotes. How can I adapt it to iterate over multiple matches on the same line?

Thanks

  • 1
    `g` - global flag: `/.../g` – Christoph Dec 15 '16 at 14:57
  • 1
    Use `var re = /"([^"]*)"/g;`. If you want leading/trailing `"`, you do not even need the parentheses. – Wiktor Stribiżew Dec 15 '16 at 14:58
  • @WiktorStribiżew ok, sounds simple but im confused and getting errors ha! i dont want the leading/trailing quotes... –  Dec 15 '16 at 15:04
  • 2
    See http://stackoverflow.com/questions/6323417/how-do-i-retrieve-all-matches-for-a-regular-expression-in-javascript how to access all captured values. If you cannot use a regex literal, use `var re = new RegExp("\"([^\"]*)\"", "g");` – Wiktor Stribiżew Dec 15 '16 at 15:06
  • @WiktorStribiżew ah ok, so i think my issues are from being able to iterate the results, thanks for the pointers. –  Dec 15 '16 at 15:13

0 Answers0