const regex = /(?msi)(pwd|password).*?\{\{(.*?)\}\}|(usr|username).*?\{\{(.*?)\}\}/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(?msi)(pwd|password).*?\\{\\{(.*?)\\}\\}|(usr|username).*?\\{\\{(.*?)\\}\\}', 'gm')
const str = `File 0:
"body": {
"mode": "raw",
"raw": "{\\n \\"partnerCode\\": \\"{{partner}}\\",\\n \\"password\\": \\"{{password}}\\",\\n \\"userName\\": \\"{{userName}}\\"\\n}"
},
"url": {
File 1:
"body": {
"mode": "raw",
"raw": "{\\n \\"partnerCode\\": \\"{{partner}}\\",\\n \\"pwd\\": \\"{{d]5!bT8**<\\fcaY?}}\\",\\n \\"usr\\": \\"{{example.user1}}\\"\\n}"
},
"url": {
File 2:
"body": {
"mode": "raw",
"raw": "{\\n \\"partnerCode\\": \\"{{partner}}\\",\\n \\"password\\": \\"{{h+Rq~%d7kQ_\\ew%]}}\\",\\n \\"usr\\": \\"{{example.user2}}\\"\\n}"
},
"url": {
File 3:
"body": {
"mode": "raw",
"raw": "{\\n \\"partnerCode\\": \\"{{partner}}\\",\\n \\"pwd\\": \\"{{Y4)8-SnkVYZ>=wKn}}\\",\\n \\"username\\": \\"{{example.user3}}\\"\\n}"
},
"url": {
File 4:
"body": {
"mode": "raw",
"raw": "{\\n \\"partnerCode\\": \\"{{partner}}\\",\\n \\"pwd\\": \\"{{,;F3QV(3~JB)grM{}}\\",\\n "\\n}"
},
"url": {`;
// 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