pistacchio pistacchio - 4 months ago 12
Javascript Question

Javascript match everything betweet delimiters, including line breaks

The following code correctly matches everything betweet

{{{
and
}}}
but the example 'line3' because the content of the brackets includes line breaks. How to match it as well?

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);
}

Answer

You can use this regex:

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

RegEx Demo

Problem is presence of a space after {{{ in your regex which is making it not match line3 line since there is a line break after {{{.

Comments