I am attempting to find the paragraph that contains a specific keyword.
Here is an example text:
In my text file, there are multiple paragraphs with varying lengths.
Each paragraph may be on multiple lines.
There is always a newline between each paragraph. I want to locate a paragraph that contains the desired keyword and match this line as well.
I don't need to match the first or last paragraphs (let's assume each paragraph has newlines around it).
Keyword: desired
(so the middle paragraph should match).
I have tried using this regex pattern:
var regX = /(.+\r?\n)+.*desired.*(?=(\r?\n)?)/igm;
This pattern currently matches the first two lines but not the last one:
There is always a newline between each paragraph. I want to locate a paragraph that contains the desired keyword.
Changing .*desired.*
to .*desired[\s\S]*
selects too much (it picks up the 2nd and 3rd paragraphs in the example) (.*desired[\s\S]*?
doesn't work either - not reluctant enough.)
Thank you for your assistance.