1

So, I have a string and I want to return an entire line when that line contains the word I'm searching for. I have this code:

var subtitle = '4'+
            '00:00:24.067 --> 00:00:35.924'+
            'Hi, how are you?'+
            'Doing fine?'+
            ''+
            '5'+
            '00:00:35.926 --> 00:00:47.264'+
            'I\'m doing the best I can.'+
            'And you?';

            var myRe = /^.* you.*$/ig;
            var myArray;
            while ((myArray = myRe.exec(legenda)) !== null) {
                var msg = 'Found ' + myArray[0] + '. ';
                msg += 'Next match starts at ' + myRe.lastIndex;
                console.log(msg);
            }

In the code above I'm trying to return the two lines that contain the word "you", the expected output would be: "Hi, how are you?" and "And you?" but I'm getting all the content of the subtitle variable. But, if I check my regular expression here I'm getting the desired output.

Could somebody help me, please? It's my first time using regular expressions and I'm feeling a bit lost.

ninjacow55
  • 27
  • 1
  • 2
  • 10

2 Answers2

3

First of all, that isn't a multiline string, you're just concatenating a string in multiple lines, but the string itself is a single line. Then when trying to match multiline strings with a regex you have to use the m flag

Check this question: Creating multiline strings in JavaScript

var subtitle = '4\n'+
            '00:00:24.067 --> 00:00:35.924\n'+
            'Hi, how are you?\n'+
            'Doing fine?\n'+
            '\n'+
            '5\n'+
            '00:00:35.926 --> 00:00:47.264\n'+
            'I\'m doing the best I can.\n'+
            'And you?\n';

            var myRe = /^.* you.*$/igm;
            var myArray;
            while ((myArray = myRe.exec(subtitle)) !== null) {
                var msg = 'Found ' + myArray[0] + '. ';
                msg += 'Next match starts at ' + myRe.lastIndex;
                console.log(msg);
            }
Community
  • 1
  • 1
Marcos Casagrande
  • 37,983
  • 8
  • 84
  • 98
1

I checked you regex link. As said by you, its giving desired results. I verified, it captures in two different groups. This site also provide code generation facility, check in left sidebar of same site?
I've done it for you in Javascript, and tested too. If you want in any other language, please generate yourself. If you face any difficulty, please comment. :)

const regex = /^.* you.*$/gmi;
const str = `4
00:00:24.067 --> 00:00:35.924
Hi, how are you?
Doing fine?

5
00:00:35.926 --> 00:00:47.264
I'm doing the best I can.
And you?
`;
let m;

while ((m = regex.exec(str)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === regex.lastIndex) {
        regex.lastIndex++;
    }

    // The result can be accessed through the `m`-variable.
    m.forEach((match, groupIndex) => {
        console.log(`Found match, group ${groupIndex}: ${match}`);
    });
}
Kamal Nayan
  • 1,890
  • 21
  • 34