const regex = new RegExp('^arn:(aws|aws-cn|aws-us-gov):([a-zA-Z0-9-]+):([a-zA-Z0-9-]*):(\\d{12}|):([^:/]+)([:/].*)?$', 'gm')
const str = `AWS
arn:aws:s3:::my-s3-bucket
arn:aws:iam::123456789012:user/JohnDoe
arn:aws:dynamodb:us-east-1:123456789012:table/UserTable
arn:aws:lambda:us-west-2:123456789012:function:ProcessData
arn:aws:ec2:us-east-1:123456789012:instance/i-0abcd1234efgh5678
GOOGLE CLOUD
projects/my-project-id/buckets/my-storage-bucket
projects/my-project-id/zones/us-central1-a/instances/instance-1
projects/my-project-id/topics/my-pubsub-topic
projects/my-project-id/datasets/my_bigquery_dataset
projects/my-project-id/instances/my-cloud-sql-instance
AZURE
/subscriptions/12345678-1234-1234-1234-123456789012/resourceGroups/MyResourceGroup/providers/Microsoft.KeyVault/vaults/MyKeyVault
/subscriptions/12345678-1234-1234-1234-123456789012/resourceGroups/MyResourceGroup/providers/Microsoft.Storage/storageAccounts/mystorageacct
/subscriptions/12345678-1234-1234-1234-123456789012/resourceGroups/MyResourceGroup/providers/Microsoft.Compute/virtualMachines/MyVM
/subscriptions/12345678-1234-1234-1234-123456789012/resourceGroups/MyResourceGroup/providers/Microsoft.Web/sites/MyAppService
/subscriptions/12345678-1234-1234-1234-123456789012/resourceGroups/MyResourceGroup/providers/Microsoft.DocumentDB/databaseAccounts/MyCosmosDB`;
// 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