const regex = /^(?<type>build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test|¯\\_\(ツ\)_\/¯)(?<scope>\(\w+\)?((?=:\s)|(?=!:\s)))?(?<breaking>!)?(?<subject>:\s.*)?|^(?<merge>Merge \w+)/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('^(?<type>build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test|¯\\\\_\\(ツ\\)_\\\/¯)(?<scope>\\(\\w+\\)?((?=:\\s)|(?=!:\\s)))?(?<breaking>!)?(?<subject>:\\s.*)?|^(?<merge>Merge \\w+)', 'gm')
const str = `## Should Pass
build: test
chore(topic): descriptoin
chore(junk): that's better
¯\\_(ツ)_/¯: descriptoin
feat: some with-hyphen
### should pass with breaking change
feat!: monkeys are the best for animal testing
build(three)!: test
Merge words
### Failed to match test
Merge
monkey(dog): no
junk
### Partials that should fail
build(with colon): must include colon. <<< fails number four
build(one) <<< must have descriptoin and must have colon
feat!:dogs are better for animal testing << give me your whitespace
build <<< thould fail, not allowed end of line
build(: <<< should fail
build(words):noSPace <<< should fail
build(two)asdfadfadasdf_this is a failure
build(<> : dda!sd): test <<< should this fail?
build(example:module)!: test subject <<< that should fail too
¯\\_(ツ)_/¯[type]:descriptoin
`;
// 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