Javascript 段落内匹配文本的正则表达式

Javascript 段落内匹配文本的正则表达式,javascript,regex,Javascript,Regex,我试图匹配包含关键字的段落 示例文本: I have a textfile containing text. Each paragraph may span multiple lines. Paragraphs have a newline between them. I would like to match a paragraph that holds some text and would like to match this line as well. The regex do

我试图匹配包含关键字的段落

示例文本:

I have a textfile containing text. Each paragraph 
may span multiple lines. 

Paragraphs have a newline between them. I would 
like to match a paragraph that holds some text
and would like to match this line as well.

The regex doesn't have to match the first or last
paragraph (we can assume each paragraph has
newlines around it). 
示例关键字:
保留
(因此中间段落应该匹配)

我尝试了以下正则表达式:
var regX=/(.+\r?\n)+.*holds.*(=(\r?\n)?)/igm

这与前两行匹配(不是最后一行):

*holds.*
更改为
*holds[\s\s]*
选择太多(在示例中选择第二段和第三段)(
*holds[\s\s]*?
也不起作用-不够贪婪。)

谢谢你的帮助。

给你:

^\r?\n(?:.+\r?\n)*.*\bholds\b.*\r?\n(?:.+\r?\n)*(?=\r?\n)
/gm
一起使用

请注意,这个regeix是受限制的,但不幸的是,在JavaScript中,您对此无能为力

此模式基本上捕获一个空行,然后是一些行(
(?:。+\r?\n)*
),然后是一个包含
的行(
*\bholds\b.\r?\n
),然后是0或更多行(
(?:。+\r?\n)*
),最后确保最后一个换行后面是一个换行:
(?=\r?\n)

^\r?\n(?:.+\r?\n)*.*\bholds\b.*\r?\n(?:.+\r?\n)*(?=\r?\n)