So basically you're trying to match a line followed by another line that contains a certain keyword, and insert a newline before it? If so, this should work:
/^.*\n.*keyword/mg
Here:
In multiline mode (enabled with the m flag) the caret (^) matches the beginning of a line.
.* matches zero or more non-newline characters, i.e. the contents of a single line.
\n matches a newline.
.*keyword again matches any number of non-newline characters followed by the literal string keyword.
You can use this regexp in a search-and-replace operation to insert the newlines like this:
let string = `line1 line2 keyword line3 line4 line6 line7 keyword line8 line9 keyword line10`; string = string.replace( /^.*\n.*keyword/gm, '\n$&' ); console.log( string );
Note that the ^ at the beginning isn't strictly needed, since most regexp engines (including any PCRE-compatible ones) will try to start each match as early as possible, and the earliest place .* can start matching something is at the beginning of a line. But having it there makes the intent clearer, and may also speed up the matching by telling the engine that there's no point in even trying to start a match in the middle of a line.
Also note that the regexp above will not match anything after keyword on the second line. For this particular replacement that doesn't matter, but if you do want to match the rest of the line, just add another .* after keyword in the regexp.
Conversely, if you know that the keyword always occurs at the beginning of the line (or if you don't actually want to match lines where it doesn't), you can remove the .* before keyword from the regexp. Or, if the keyword must occur alone on the line, you can remove the .* before keyword and add an end-of-line anchor ($) after it.
Ps. as bitinerant notes, the online search-and-replace tool you're using doesn't support escape sequences like \n in the replacement. To work around those limitations, you can use the regexp /^.*(\n).*keyword/mg and the replacement string $1$&. Here, the parenthesized subexpression (\n) just matches the newline between the two lines, so that we can use $1 in the replacement to insert an extra newline without having to somehow enter a literal newline into the replacement field.