const regex = /([\-]*BEGIN\sCERTIFICATE\sREQUEST[\-]+.+[-]+END\sCERTIFICATE\sREQUEST[-]+)/gmysi;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('([\\-]*BEGIN\\sCERTIFICATE\\sREQUEST[\\-]+.+[-]+END\\sCERTIFICATE\\sREQUEST[-]+)', 'gmysi')
const str = `-----BEGIN CERTIFICATE REQUEST-----
MIIEuDCCAqACAQAwczELMAkGA1UEBhMCVVMxGDAWBgNVBAMMD3BsdWdpYmlsaXR5
LmNvbTEXMBUGA1UEBwwOU2FsdCBMYWtlIENpdHkxFDASBgNVBAoMC1BsdWdpYmls
aXR5MQ0wCwYDVQQIDARVdGFoMQwwCgYDVQQLDANPcmcwggIiMA0GCSqGSIb3DQEB
AQUAA4ICDwAwggIKAoICAQCfQqx8YexJ4HjGqIpywxvTOPDnqIaZicD3IQSUdAb5
5vcfAaD/0KBH/6Oa2NPlq4mYAw2octDiuklvkXIUgnn85vwlj6fLm6iR6i1tjM3g
tusxur3fmMdF9YqDNNM7LVSxXMq/vXuChPj3bVib2EyKJRcBn8EMTnJYdfl3YK4l
j+KW7cnxIy8E3DAy4KIUPbI2LNPix43emW6XuMB3hpvcbs2YpQogagAQwvNYAIcW
Ub3+fSEbXFNVmYFRPbd7T5Arei4Ubl8kKVWfqUnWIOdYQh7o0CbQl+67ivAyRnDU
oItNtPz+seWzL1xerjQqdlHrCQFGNfLdI3Hfl9SZVKOqdsjs2bEiP+C6E/pilvu/
zEtD5VbGd0UBBZtANm6BdBEhhigX8vL/FfiqxlPsEK31L9tQnwa6ETvcVbRQoXBW
idJJKCmiScz17ML8aTxw0zXc16CBHuE4AEV7xaNmdl5NsYCB+q/H3SCVsreMnu7A
cpCgvaCZX1/8zSWrVNMf0i+/gJCoBh1OXNnqtCPycJDPvrVGYF2U7RqzC4c91Kqe
QxEKHApEJAL47nFtM9c3K2fNvLEVOD+k+iDkEuL++VHaXO3VHCiVoJjQCSNTChl3
aSL29LadjcPvf2QdM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hxsi4OHtKwHp3YraUXIOx
ZR+Izt85BFUSJKhO5vj91RYHPLOOX1eaJ1xX7lLP0za6oA1Djpkt8ypC1Grushk5
PGwud6U6Dd3BM1gdEP7wWeELOPXJIMLo2SApAu8NAoIBAGjYmPl9gJ/ZEN4KOyE6
4UJ5VME6RNusJzX1+TJ6qtU+zEH3C9x8EsKALOPcwDvye1VWGNGpFYa1ylg709tF
W/1mvOKhOnay2wRjNyMyxRgWpHyeAxN/fUmCpytnb2RaZgktGht8TfSYs5a53HZC
GJC5TO1u5kh1STjtgGtWqCwwA/VrtZf6+pTxHo3FoQHEBWHNzIUFw65wJbBF6hC0
wDO7/ojLrFHcpjldqIMd5pt4F90O4iDchxcfD0J1DfW0cXuGdJDlxafk3tT4CpJ7
Dxrm7xO8vkrMqYvCfNSem8LxJ+MSnauvBlg4moYkdGCJAimkEkRpV0VK4bjjGrlu
0YMCggEAGftF1dJBqkUKc9dsbSQYwwfaU+W/gFWU4otTPd89HIOVkKyz3Hc0FrEH
68Ffrlx1MZU8wbNjjXytujJ7vwn0+n0bXN+5/oqEM+zlVunefCCLWWVLD0Sc/yOx
llgrU9u2z8Zjjl+mbbWNKhjV6oBNw3qi4CNHiBMHQk98xGQaRaTh/t/laXxZgtIo
Y+mZN7OXk3IUv6xlO+OyneG+CU9/VOPszLK9yFf95m9l5R6E6v9r4eE9N5OMOTxT
SBXZ+zNnnF4bIjy747KtCVI7XJQURDBbhhXick/lgX6sNj87vMaDJDnZ9kqqgc3U
1H23mg7nAvcEhsYClM3Z8/bEH6eRBQ==
-----END PRIVATE KEY-----
`;
// 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