The text editor 'scan' is not returning all matches


#1

Hi,

I am creating some code to scan a text buffer and replace what I find. I created the test code below:

const dummyEditor = atom.workspace.buildTextEditor();
dummyEditor.insertText('126\n');
dummyEditor.insertText('00:01:04,620 --> 00:01:07,000\n');
dummyEditor.insertText('Lorem ipsum\n');
dummyEditor.insertText('\n');
dummyEditor.insertText('127\n');
dummyEditor.insertText('00:01:07,200 --> 00:01:08,000\n');
dummyEditor.insertText('dolor sit amet');

dummyEditor.scan(new RegExp('^\\d+:\\d+:\\d+,\\d+ --> \\d+:\\d+:\\d+,\\d+$'), iterator => {
  console.log('Match! ' + iterator.match);
});

I’ve tested the regular expression and it is working. The idea is that it will return to me these two lines:

00:01:04,620 --> 00:01:07,000
00:01:07,200 --> 00:01:08,000

However I am just getting back the first line, even though the Atom documentation says that the scan method will iterate through the text buffer until either someone calls stop() or no more matches are found.

Am I missing something? What am I doing wrong?


#2

Hi,

I solved my problem! The issue was that I forgot to add a 'g' option for my regular expression. The updated regex is below:

dummyEditor.scan(new RegExp('^\\d+:\\d+:\\d+,\\d+ --> \\d+:\\d+:\\d+,\\d+$', 'g'), iterator => {
  console.log('Match! ' + iterator.match);
});