2016-08-02 67 views
1

以下代碼正確匹配{{{}}}之間的所有內容,但示例'line3'因爲括號內容包含換行符。如何配合呢?Javascript匹配包括換行符在內的所有內容

const testcase = ` 
    line1: {{{ content1 }}} 
    line2: {{{ content2 }}} 
    line3: {{{ 
     content3 
    }}} 
    line4: {{{ content4 }}} 
`; 

const regex = /^(\s+)(.*?)(\{\{\{ [^]*? \}\}\})/gm; 

let match; 
while ((match = regex.exec(testcase)) != null) { 
    console.log(match); 
} 

回答

2

你可以使用這個表達式:

/^(\s+)(.*?)({{{[^]*?}}})/gm 

RegEx Demo

問題是在你的正則表達式{{{其正在因爲有一個換行符它不匹配line3線後空間的存在之後{{{

相關問題