用于在JavaScript中捕获heredoc的正则表达式

用于在JavaScript中捕获heredoc的正则表达式,javascript,regex,heredoc,Javascript,Regex,Heredoc,我有一个perl脚本,例如如下所示: #/usr/bin/perl -w print 'My output: '; print <<END; Here is more content which is printed with heredoc style END print 'End of output'; #/usr/bin/perl-w 打印“我的输出:”; 打印问题在于默认情况下,*是“贪婪的”*捕获它可以匹配的所有内容,直到*之前的模式失败。只有到那时它才会回来。在

我有一个perl脚本,例如如下所示:

#/usr/bin/perl -w

print 'My output: ';

print <<END;
Here is more content 
which is printed with
heredoc style
END

print 'End of output';
#/usr/bin/perl-w
打印“我的输出:”;

打印问题在于默认情况下,
*
是“贪婪的”<代码>*
捕获它可以匹配的所有内容,直到
*
之前的模式失败。只有到那时它才会回来。在您的例子中,模式一直有效到字符串的末尾

为了防止它贪婪,并检查它是否通过了应该结束的点(参见我在那里做了什么?:D),在
*
之后添加

<<END(.|\n)*?END

问题在于默认情况下,
*
是“贪婪的”<代码>*
捕获它可以匹配的所有内容,直到
*
之前的模式失败。只有到那时它才会回来。在您的例子中,模式一直有效到字符串的末尾

为了防止它贪婪,并检查它是否通过了应该结束的点(参见我在那里做了什么?:D),在
*
之后添加

<<END(.|\n)*?END

很好的解决方案。您认为可以捕获一个herdeoc字符串而不指定herdeoc字符串
END
?@BenjaminJ。寻找“反向参考”。非常感谢。我的解决方案是
不错的解决方案。您认为可以捕获一个herdeoc字符串而不指定herdeoc字符串
END
?@BenjaminJ。寻找“反向参考”。非常感谢。我的解决方案是

<<END;
Here is more content 
which is printed with
heredoc style
END

print <<END;
Here is even more content 
which is printed with
heredoc style
END
<<END;
Here is more content 
which is printed with
heredoc style
END
<<END;
Here is even more content 
which is printed with
heredoc style
END
<<END(.|\n)*?END