在 JavaScript 中捕获 heredoc 的正则表达式
regex for catching heredoc in JavaScript
我有一个 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';
现在我想用JavaScript提取上面heredoc打印的内容。结果应如下所示:
<<END;
Here is more content
which is printed with
heredoc style
END
我已经用 <<END(.|\n)*END
试过了。如果文档仅包含一个 heredoc,则此方法有效,但如果它包含多个 heredoc,则无效。
例如,如果我的 perl 脚本如下所示:
#/usr/bin/perl -w
print 'My output: ';
print <<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
print 'End of output';
正则表达式匹配:
<<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
有人知道我的正则表达式有什么问题吗?
另一个问题:是否可以仅使用正则表达式来捕获所有未指定到 heredoc 字符串的 heredocs END
?
问题是 *
默认为 "greedy"。 *
捕获它可以匹配的所有内容,直到 *
之前的模式失败。只有这样 return。在您的情况下,该模式一直有效到字符串末尾。
为了防止它变得贪婪并检查它是否通过了它应该结束的点(看到我在那里做了什么?:D),在 *
.[=16= 之后添加 ?
]
<<END(.|\n)*?END
我有一个 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';
现在我想用JavaScript提取上面heredoc打印的内容。结果应如下所示:
<<END;
Here is more content
which is printed with
heredoc style
END
我已经用 <<END(.|\n)*END
试过了。如果文档仅包含一个 heredoc,则此方法有效,但如果它包含多个 heredoc,则无效。
例如,如果我的 perl 脚本如下所示:
#/usr/bin/perl -w
print 'My output: ';
print <<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
print 'End of output';
正则表达式匹配:
<<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
有人知道我的正则表达式有什么问题吗?
另一个问题:是否可以仅使用正则表达式来捕获所有未指定到 heredoc 字符串的 heredocs END
?
问题是 *
默认为 "greedy"。 *
捕获它可以匹配的所有内容,直到 *
之前的模式失败。只有这样 return。在您的情况下,该模式一直有效到字符串末尾。
为了防止它变得贪婪并检查它是否通过了它应该结束的点(看到我在那里做了什么?:D),在 *
.[=16= 之后添加 ?
]
<<END(.|\n)*?END