I am trying to extract multiple strings using different patterns from one long string. I have done this successfully using different Regex queries but I feel this is not very efficient. Here is an example of the input string:
const input = ....Number of students: 5...[New]Break at 1:45 pm\nStudents involved are: John, Joseph, Maria\nLunch at 2:00 pm...Activities remaining: long jump, shuffle..
There are three prefixes which are used to extract the data after it:
const prefix1 = Students involved are:
const prefix2 = Activities remaining:
const prefix3 = Number of students:
The only pattern is a newline after each of the above three strings. Example: Students involved are: John, Joseph, Maria\n
I have used the following regex to accomplish this:
const students = input.match(new RegExp(prefix1 + "(.*)"));
const activities = input.match(new RegExp(prefix2 + "(.*)"));
const numOfStudents = input.match(new RegExp(prefix3 + "(.*)"));
Is there a better and more efficient way of accomplishing the above where I have to iterate through the long string only once?