0

I have a string and want to match:

await Listing.find({
    Test: 1
});

But don’t want to match it if it ends with .lean();

await Listing.find({
    Test: 1
}).lean();

I have this regex but it’s not working: (?<=await)([\S\s]*?)(?!.+\.lean())(;)

4 Answers 4

1

You can use this modified regex:

(?<=await)([\S\s]*?)(?<!.+\.lean\(\))(;)

All I have changed is:

Making \.lean a negative look BEHIND

ESCAPING the parentheses.

Sign up to request clarification or add additional context in comments.

Comments

1
^await(?:(?!\.lean\(\)).)*;$

Short Explanation

  • ^await String starts with await
  • (?:(?!\.lean\(\)).)*; Contains anything except .lean() till the last ; colon

Also, see the regex demo

JavaScript Example

let regex = /^await(?:(?!\.lean\(\)).)*;$/s;

console.log(regex.test(`await Listing.find({
    Test: 1
});`));

console.log(regex.test(`await Listing.find({
    Test: 1
}).lean();`));

Comments

1

If you want to stay between a single opening and closing parenthesis, you don't need any assertion:

\bawait\s[^()]*\([^()]*\);

Regex demo

Comments

0

I ended up using this in vscode to find all mongoose queries that don't use lean so that I can increase the speed of queries and reduce memory footprint:

await (.*)find(([^;]|\n)*?)(?<!.\.lean\(\))(;)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.