如何结合以下正则表达式?

时间:2015-01-16 17:23:11

标签: javascript html regex

所以基本的想法是我想将以下内容分为3个部分,描述文本,html代码和脚本代码:

---
name: Accordion
description: |
  ### What
  Puts modules of content into a linear stack of panels that can be closed and opened independently of each other. The accordion also helps in shortening a page and making it more scannable.

  ### Use when
  The content modules are mutually exclusive. Content modules can consist of a list of items, links or text blocks. 
---
<!-- Accordion-->
<div class="ui-accordion">
    some html
</div>
<!-- / Accordion  -->

<script>
some code
</script>

目前我已经能够将它分成两部分,但它还包括需要在第三部分的脚本部分。

^(-{3}(?:\n|\r)([\w\W]+?)-{3})?([\w\W]*)

我试过这种方式,但工作

^(-{3}(?:\n|\r)([\w\W]+?)-{3})?([\w\W]*)(<script.*?>([\s\S]*?)<\/script>)

有什么建议吗?

我需要的确切输出如下:

### What
      Puts modules of content into a linear stack of panels that can be closed and opened independently of each other. The accordion also helps in shortening a page and making it more scannable.

      ### Use when
      The content modules are mutually exclusive. Content modules can consist of a list of items, links or text blocks. 

<!-- Accordion-->
<div class="ui-accordion">
    some html
</div>
<!-- / Accordion  -->

some code

但它需要分为3组。

1 个答案:

答案 0 :(得分:0)

试试这个:

(?:---[\s\n\r]*((?:.|[\n\r\s])*)(?=[\s\r\n]+---)[\s\r\n]+---)?(?:(?=[\n\r\s]+<!--[\s]*Accordion[\s]*-->)[\s\r\n]+<!--[\s*]Accordion[\s]*-->[\s\n\r]*((?:.|[\n\r\s])*)(?=[\s\r\n]*<!--[\s]*\/ Accordion[\s]*-->)[\s\r\n]+<!--[\s]*\/ Accordion[\s]*-->)?(?:(?:.|[\n\r\s])*(?=<script>)<script>[\s\r\n]*((?:.|[\n\r\s])*)(?=[\s\r\n]*<\/script>)[\s\r\n]+<\/script>)?

请注意,这对输入中的一些内容是严格的(注释用&#34; ---&#34;,注释开始/结束)。可能需要调整以适应更自由的比赛。