const regex = /filename="Myfile\.pdf"\s*(\S.*(?:\n\S.*)*)/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('filename="Myfile\\.pdf"\\s*(\\S.*(?:\\n\\S.*)*)', 'gm')
const str = `--------------A5B0A8B4F69F8BD959B758D0
Content-Type: application/pdf;
name="Myfile.pdf"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename="Myfile.pdf"
JVBERi0xLjQKJeLjz9MKMyAwIG9iago8PC9UeXBlL1hPYmplY3QvQ29sb3JT
ZWI0ODRmNDE1ZDE0YmIyZmU2YjAzZDMzNjU+PGU5MDFiZTMzY2FlOTY4ZDM2
NmFmOGNhOTUxNTE0Nzk0Pl0vSW5mbyAyMyAwIFIvU2l6ZSAyND4+CnN0YXJ0
eHJlZgoyMzg2NgolJUVPRgo=
--------------A5B0A8B4F69F8BD959B758D0--
--------------A5B0A8B4F69F8BD959B758D0
Content-Type: application/pdf;
name="Myfile.pdf"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename="Myfile.pdf"
JVBERi0xLjQKJeLjz9MKMyAwIG9iago8PC9UeXBlL1hPYmplY3QvQ29sb3JT
ZWI0ODRmNDE1ZDE0YmIyZmU2YjAzZDMzNjU+PGU5MDFiZTMzY2FlOTY4ZDM2
NmFmOGNhOTUxNTE0Nzk0Pl0vSW5mbyAyMyAwIFIvU2l6ZSAyND4+CnN0YXJ0
eHJlZgoyMzg2NgolJUVPRgo=
--------------A5B0A8B4F69F8BD959B758D0--`;
// Reset `lastIndex` if this regex is defined globally
// regex.lastIndex = 0;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for JavaScript, please visit: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions