const regex = /^(?<type>revert)(?:\((?<scope>[a-z-]+)\))?(?<breaking>!)?: (?:|(?:.+\n\n(?:- )?))(?:(?:(?:(?<owner>[\d*a-z~-][\w*.~-]*)\/(?<repository>[\da-z~-][\w.~-]*))|(?<user>[\da-z~-][\w.~-]*))@)?(?<hash>[\da-z]+)/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('^(?<type>revert)(?:\\((?<scope>[a-z-]+)\\))?(?<breaking>!)?: (?:|(?:.+\\n\\n(?:- )?))(?:(?:(?:(?<owner>[\\d*a-z~-][\\w*.~-]*)\\\/(?<repository>[\\da-z~-][\\w.~-]*))|(?<user>[\\da-z~-][\\w.~-]*))@)?(?<hash>[\\da-z]+)', 'gm')
const str = `revert(release-alpha)!: flex-development/mlly@0f2fb6e2a5a11c6558c48bffa02ae881999cfd8d
revert: flexdevelopment@0f2fb6e2a5a11c6558c48bffa02ae881999cfd8d
revert: 0f2fb6e2a5a11c6558c48bffa02ae881999cfd8d
revert(release): "release: 1.0.0-alpha.2"
- 0f2fb6e2a5a11c6558c48bffa02ae881999cfd8d
revert(release): "release: 1.0.0-alpha.2"
flexdevelopment@0f2fb6e2a5a11c6558c48bffa02ae881999cfd8d`;
// 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