lolol
lolol

Reputation: 4390

regex to match terms in multiple lines

I have been using a positive and negative lookahead regex to match occurrences in a string, like:

/^(?=.*term1)(?=.*term2)(?!.*term3).*$/

Now I have a new requirement to match those strings across multiple lines. Seems like the m modifier will match those terms for each line, not exactly what i need.

Am I missing something? Is there another solution?

Upvotes: 2

Views: 1453

Answers (2)

anubhava
anubhava

Reputation: 784878

You can use this regex to match across the lines in Javascript:

/^(?=[^]*term1)(?=[^]*term2)(?![^]*term3)[^]*$/

In JS, [^] matches any character including new line.

RegEx Demo

If not using JS or want to make this regex portable to other flavors then one can use:

/^(?=[\D\d]*term1)(?=[\D\d]*term2)(?![\D\d]*term3)[\D\d]*$/

Upvotes: 2

Sterling
Sterling

Reputation: 332

Try removing the "^" and "$" From your regex, like

/(?=.*term1)(?=.*term2)(?!.*term3).*/

"^" denotes start of line and "$" denotes end of line, so this will only ever match one line.

Upvotes: -1

Related Questions