简体   繁体   English

javascript正则表达式,以匹配2个html注释标签之间的所有内容

[英]javascript regex to match everything between 2 html comment tags

I have to create a regex which will match anything (including line breaks) between 2 html specific comment tags. 我必须创建一个正则表达式,它将匹配2个html特定注释标签之间的所有内容(包括换行符)。 Here is an example for some html: 这是一些html的示例:

<!-- build-remove-start -->
<!-- bower:js -->
<script src="../bower_components/jquery/dist/jquery.js"></script>
<!-- endbower -->
<!-- build-remove-end -->

<!-- inject:vendor:js -->
<!-- endinject -->

I would like to match every thing between <!-- build-remove-start --> and <!-- build-remove-end --> not including the comment tags (I mean not to remove the comments tags, only what's inside). 我想匹配<!-- build-remove-start --><!-- build-remove-end -->不包括注释标签(我的意思是不删除注释标签,只删除那些内)。
I have tried: 我努力了:
<!-- build-remove-start -->[\\s\\S]<!-- build-remove-end -->
But it doesn't work. 但这是行不通的。 Here is a link to test the regex: 这是测试正则表达式的链接:
https://regex101.com/r/rV0qG2/2 https://regex101.com/r/rV0qG2/2

You need to add * after [\\s\\S] . 您需要在[\\s\\S]之后添加*

/<!-- build-remove-start -->([\s\S]*?)<!-- build-remove-end -->/gmi

Also I recommend adding ? 我也建议添加? after * so it makes it lazy. *之后,因此使其变得懒惰。 This way you can have the example below, othwerise the this won't be removed line will be matched. 这样,您可以在下面的示例中进行操作, this won't be removed

<!-- build-remove-start -->
//remove this
<!-- build-remove-end -->

This won't be removed

<!-- build-remove-start -->
//Remove this
<!-- build-remove-end -->

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM