const regex = /^\w++\W++((?<=\")[\w-]+(?="))\W+((?<=\")[\w-]+(?="))[\S\s]+?(count\W+=.*)[\S\s]+?^}$/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('^\\w++\\W++((?<=\\")[\\w-]+(?="))\\W+((?<=\\")[\\w-]+(?="))[\\S\\s]+?(count\\W+=.*)[\\S\\s]+?^}$', 'gm')
const str = `resource "aws_iam_role" "my_test_payer_specific_role" {
count = "\${(var.payer_account == "380834257621")?1:0}"
name = "my_test_payer_specific_role"
max_session_duration = "7200"
assume_role_policy = "\${data.aws_iam_policy_document.my_test_payer_specific_role-policy.json}"
tags = "\${var.resources_tags}"
lifecycle {
ignore_changes = [
"permissions_boundary"
]
}
}
resource "aws_iam_role_policy_attachment" "my_test_payer_specific_role-policy-attachment" {
role = "\${aws_iam_role.my_test_payer_specific_role.name}"
count = "\${(var.payer_account == "380834257621")?1:0}"
policy_arn = "arn:\${var.partition}:iam::aws:policy/AdministratorAccess"
}
data "aws_iam_policy_document" "my_test_payer_specific_role-policy" {
count = "\${(var.payer_account == "380834257621")?1:0}"
statement {
actions = ["sts:AssumeRole"]
principals {
type = "AWS"
identifiers = ["arn:\${var.partition}:iam::\${var.payer_account}:root", "arn:\${var.partition}:iam::380834257621:root"]
}
}
}`;
// 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