Skip to content

Instantly share code, notes, and snippets.

@donhector
Last active November 29, 2021 02:28
Show Gist options
  • Save donhector/9b4139f1db682a77c9cdd3110e259505 to your computer and use it in GitHub Desktop.
Save donhector/9b4139f1db682a77c9cdd3110e259505 to your computer and use it in GitHub Desktop.
Conventional Commits Regex

Conventional Commit Regex

^(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test)(\(\w+((,|\/|\\)?\s?\w+)+\))?!?: [\S ]{1,49}[^\.]$

This regex enforces the rules for the header line of the conventional commit specification.

  • Supports the standard types
  • Supports the breaking changes exclamation mark notation
  • Enforces using up to 50 chars for the commit description
  • Enforces not ending the commit description with a dot.
  • Supports optional scope(s)
  • Multiple scopes can be delimited by space, comma, forward slash, or backward slash (similar to commitlint)

Matching examples:

ci: Most basic use case
fix(api): Valid commit with optional scope
feat!: Breaking change example
feat(scope1 scope2): Multiple space separated scopes
feat(scope1, scope2)!: Comma separated scopes with breaking change
feat(scope1,scope2): Comma separated scopes without space
fix(scope1/scope2): Fwd slash without spaces
fix(scope1\scope2): Bwd slash without spaces

Non matching examples:

fix: This description is over 50 characters loooooooooooong
fix: Ending with a dot.
fix:No spacing before description
fix!(scope): Bad breaking change placement
fix (api): Invalid space between type and scope
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment