const regex = new RegExp('^(?:/api|/apis/(?P<GROUP>[^/]+))/(?P<VERSION>[^/]+)(?:/namespaces/(?P<NAMESPACE>[^/]+))?/(?P<RESOURCETYPE>[^/\\n]+)(?:/(?P<NAME>[^/\\n]+))?(?:/(?P<SUBRESOURCE>[^/\\n]+))?$', 'gm')
const str = `## Cluster-scoped resources
/api/v1/RESOURCETYPE
/api/v1/RESOURCETYPE/NAME
/apis/GROUP/VERSION/RESOURCETYPE
/apis/GROUP/VERSION/RESOURCETYPE/NAME
## Namespace-scoped resources:
/api/v1/namespaces/NAMESPACE/RESOURCETYPE
/api/v1/namespaces/NAMESPACE/RESOURCETYPE/NAME
/apis/GROUP/VERSION/namespaces/NAMESPACE/RESOURCETYPE
/apis/GROUP/VERSION/namespaces/NAMESPACE/RESOURCETYPE/NAME
## Cluster-scoped subresource:
/api/v1/RESOURCETYPE/NAME/SUBRESOURCE
/apis/GROUP/VERSION/RESOURCETYPE/NAME/SUBRESOURCE
## Namespace-scoped subresource
/api/v1/namespaces/NAMESPACE/RESOURCETYPE/NAME/SUBRESOURCE
/apis/GROUP/VERSION/namespaces/NAMESPACE/RESOURCETYPE/NAME/SUBRESOURCE`;
// 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