RegExp testing fails when string is validated in RegExp tools

I have a small regexp that should verify if a commits subject adheres to the ReactJS commit message format. Since the expression works with my test strings the code left me baffled.

This small example should reproduce the behaviour:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>#!/bin/bash
function test_subject {
local subject="$1"
local pattern="^(feat|fix|docs|style|refactor|test|chore)([a-zA-Z0-9._-]+): [^n]+$"
if ! [[ $subject =~ $pattern ]]; then
echo "Invalid subject: $subject"
else
echo " Valid subject: $subject"
fi
}
test_subject "chore(gh-actions): add script for commit check"
test_subject "chore(gh-actions): add script for commit checking"
test_subject "feat(ABC-123): add new feature"
test_subject "fix(ABC123): add new feature"
test_subject "fix(ABC123): fix previously added feature"
test_subject "fix(scope): fix bug"
</code>
<code>#!/bin/bash function test_subject { local subject="$1" local pattern="^(feat|fix|docs|style|refactor|test|chore)([a-zA-Z0-9._-]+): [^n]+$" if ! [[ $subject =~ $pattern ]]; then echo "Invalid subject: $subject" else echo " Valid subject: $subject" fi } test_subject "chore(gh-actions): add script for commit check" test_subject "chore(gh-actions): add script for commit checking" test_subject "feat(ABC-123): add new feature" test_subject "fix(ABC123): add new feature" test_subject "fix(ABC123): fix previously added feature" test_subject "fix(scope): fix bug" </code>
#!/bin/bash

function test_subject {
  local subject="$1"
  local pattern="^(feat|fix|docs|style|refactor|test|chore)([a-zA-Z0-9._-]+): [^n]+$"

  if ! [[ $subject =~ $pattern ]]; then
    echo "Invalid subject: $subject"
  else
    echo "  Valid subject: $subject"
  fi
}

test_subject "chore(gh-actions): add script for commit check"
test_subject "chore(gh-actions): add script for commit checking"
test_subject "feat(ABC-123): add new feature"
test_subject "fix(ABC123): add new feature"
test_subject "fix(ABC123): fix previously added feature"
test_subject "fix(scope): fix bug"

This leads to the following output:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> Valid subject: chore(gh-actions): add script for commit check
Invalid subject: chore(gh-actions): add script for commit checking
Invalid subject: feat(ABC-123): add new feature
Invalid subject: fix(ABC123): add new feature
Valid subject: fix(ABC123): fix previously added feature
Valid subject: fix(scope): fix bug
</code>
<code> Valid subject: chore(gh-actions): add script for commit check Invalid subject: chore(gh-actions): add script for commit checking Invalid subject: feat(ABC-123): add new feature Invalid subject: fix(ABC123): add new feature Valid subject: fix(ABC123): fix previously added feature Valid subject: fix(scope): fix bug </code>
  Valid subject: chore(gh-actions): add script for commit check
Invalid subject: chore(gh-actions): add script for commit checking
Invalid subject: feat(ABC-123): add new feature
Invalid subject: fix(ABC123): add new feature
  Valid subject: fix(ABC123): fix previously added feature
  Valid subject: fix(scope): fix bug

1

You will need to use . instead of [^n] in your shell regex to match any character.

[^n] is being evaluated as [^n] i.e. any character other than n and your example strings number 2, 3, and 4 have the letter n somewhere after matching :.

This should work for you:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>test_subject() {
local subject="$1"
local pattern="^(feat|fix|docs|style|refactor|test|chore)([a-zA-Z0-9._-]+): .+$"
if ! [[ $subject =~ $pattern ]]; then
echo "Invalid subject: $subject"
else
echo " Valid subject: $subject"
fi
}
test_subject "chore(gh-actions): add script for commit check"
test_subject "chore(gh-actions): add script for commit checking"
test_subject "feat(ABC-123): add new feature"
test_subject "fix(ABC123): add new feature"
test_subject "fix(ABC123): fix previously added feature"
test_subject "fix(scope): fix bug"
</code>
<code>test_subject() { local subject="$1" local pattern="^(feat|fix|docs|style|refactor|test|chore)([a-zA-Z0-9._-]+): .+$" if ! [[ $subject =~ $pattern ]]; then echo "Invalid subject: $subject" else echo " Valid subject: $subject" fi } test_subject "chore(gh-actions): add script for commit check" test_subject "chore(gh-actions): add script for commit checking" test_subject "feat(ABC-123): add new feature" test_subject "fix(ABC123): add new feature" test_subject "fix(ABC123): fix previously added feature" test_subject "fix(scope): fix bug" </code>
test_subject() {
  local subject="$1"
  local pattern="^(feat|fix|docs|style|refactor|test|chore)([a-zA-Z0-9._-]+): .+$"

  if ! [[ $subject =~ $pattern ]]; then
    echo "Invalid subject: $subject"
  else
    echo "  Valid subject: $subject"
  fi
}

test_subject "chore(gh-actions): add script for commit check"
test_subject "chore(gh-actions): add script for commit checking"
test_subject "feat(ABC-123): add new feature"
test_subject "fix(ABC123): add new feature"
test_subject "fix(ABC123): fix previously added feature"
test_subject "fix(scope): fix bug"

Output:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Valid subject: chore(gh-actions): add script for commit check
Valid subject: chore(gh-actions): add script for commit checking
Valid subject: feat(ABC-123): add new feature
Valid subject: fix(ABC123): add new feature
Valid subject: fix(ABC123): fix previously added feature
Valid subject: fix(scope): fix bug
</code>
<code>Valid subject: chore(gh-actions): add script for commit check Valid subject: chore(gh-actions): add script for commit checking Valid subject: feat(ABC-123): add new feature Valid subject: fix(ABC123): add new feature Valid subject: fix(ABC123): fix previously added feature Valid subject: fix(scope): fix bug </code>
Valid subject: chore(gh-actions): add script for commit check
Valid subject: chore(gh-actions): add script for commit checking
Valid subject: feat(ABC-123): add new feature
Valid subject: fix(ABC123): add new feature
Valid subject: fix(ABC123): fix previously added feature
Valid subject: fix(scope): fix bug

1

Bash regexp does not know n to be a new line character. The [^n] is just a [^n] so the lines which your script marks as invalid – have “n” in them (“checking”, “new”).

The new line bash regexp do recognize is actually $ – end of the line.

Another point – your test strings do not have n characters in them, so there is no real need to check for its absence – hence a simple <complex_pattern>: .+ is enough, which would mean that complex pattern ends with colon, space and you have something after that. No real need to end the pattern with $ since it already will go up to the end of a string.

Said that… if your just hit Enter inside the pattern, you will get a string like:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> local pattern="^(feat|fix|docs|style|refactor|test|chore)([a-zA-Z0-9._-]+): [^
]+$"
</code>
<code> local pattern="^(feat|fix|docs|style|refactor|test|chore)([a-zA-Z0-9._-]+): [^ ]+$" </code>
  local pattern="^(feat|fix|docs|style|refactor|test|chore)([a-zA-Z0-9._-]+): [^
]+$"

But bush (or at least some versions of it) will see it as a single string with a n and do a real “no n inside the string”.

It is possible – yes. Is it better than simple .+ – definitely not. But as a funny feature and possible way to confuse people – yes.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật