2016-04-19 11 views
0

この値下げファイル想像:部分一致を無視する正規表現を書くには?

... 

## Questions heading 
### Question sub-heading 
- some question 
- some question 
### Question sub-heading 
- some question 

## Next section heading 
- blah 
- blah 

## Another section heading 
- blah 
- blah 

を私は時々質問小見出しを含むことが質問セクションでの質問のすべてを抽出することができるようにする必要があります。

私の正規表現は、sub-heading doesn't existの場合にのみ動作します。ここに私の現在の正規表現は次のようになります。上記の例のためにこれを返します##\sQuestions([\s\S]*?)##

## Questions heading 
## 

私はそれは二つの主な見出しの間のセクション全体を返却する必要があります。これは次のようになります。私はそれらをメインセクションの見出しではなく、次の主要なセクションの見出しが始まるまで##と表記され、マッチングを続けて### a.k.a小見出しを無視する必要が

### Question sub-heading 
- some question 
- some question 
### Question sub-heading 
- some question 

答えて

0

これはトリックを行う必要があり、私はそれはかなりだとは言わないよ、それは動作します:

/^##\s*Questions.*?\n([^]*?)^##[^#]/m 

テスト:それは##に頼っています

var match = `## Questions heading 
### Question sub-heading 
- some question 
- some question 
### Question sub-heading 
- some question 

## Next section heading 
- blah 
- blah 

## Another section heading 
- blah 
- blah`.match(/^##\s*Questions.*?\n([^]*?)^##[^#]/m); 
if (match) { 
    console.log(match[1]); 
} 

は、行の先頭にあります。

内訳:

/ 
    ^##\s*Questions.*?\n # Match "## Questions ...\n" 
    ([^]*?)    # Match anything including newline 
    ^##[^#]    # Match "## ..." 
/m      # Make `^` and `$` work on each line instead of all input 
0

あなたはそれが(?!#)を追加して、別の#が続かない時はいつでもだけ\n##に一致するようにnegative look-aheadを使用することができます:あなたは\n##だけではなく##を一致させる必要が

##\sQuestions([\s\S]*?)\n##(?!#)

注意。改行にマッチすると、正規表現は###と一致します。最初の#[\s\S]の一部としてマッチします。

関連する問題