Skip to content

fix: use word-boundary lookbehind/lookahead for trigger phrase matching#991

Open
yoheidemachi wants to merge 4 commits intoanthropics:mainfrom
yoheidemachi:fix/trigger-phrase-boundary
Open

fix: use word-boundary lookbehind/lookahead for trigger phrase matching#991
yoheidemachi wants to merge 4 commits intoanthropics:mainfrom
yoheidemachi:fix/trigger-phrase-boundary

Conversation

@yoheidemachi
Copy link

@yoheidemachi yoheidemachi commented Mar 1, 2026

Summary

Closes #941
Also addresses #910 (case-insensitive matching)

The previous regex used an explicit character allowlist for boundaries (e.g. (^|\s) / ([\s.,!?;:]|$)), which silently rejected common patterns like (@claude), "@claude", >@claude, and cc:@claude. Each new punctuation character required a manual allowlist update.

This PR replaces the allowlist approach with negative lookbehind/lookahead using (?<![\w-]) and (?![\w-]):

  • Any non-word, non-hyphen character is accepted as a boundary — no explicit allowlist needed
  • Hyphenated usernames like @claude-bot correctly do NOT trigger (preserves existing behavior)
  • email@claude.com correctly does NOT trigger (word character l blocks the match)
  • Case-insensitive matching via the "i" flag (fixes trigger_phrase matching is case-sensitive, should be case-insensitive #910)

Additionally:

  • Extract buildTriggerRegex() helper to eliminate 4× duplicated regex construction
  • Update docs/faq.md to accurately reflect the expanded matching behavior

Before

Comment Triggers?
(@claude) can you check? No
"@claude can you check?" No
>@claude can you check? No
cc:@claude check this No
`@claude` can you check? No
@Claude help No

After

All of the above now trigger correctly.

Still correctly rejected:

  • email@claude.com (word character before @)
  • @claude-bot (hyphen continues the token)
  • claudette (word character after e)

Test plan

  • All tests pass (bun test) — 27 tests, 67 assertions
  • TypeScript typecheck passes (bun run typecheck)
  • Prettier format check passes (bun run format:check)
  • Added test cases for punctuation-preceded mentions: (, ", >, ', /, :, `, [, **
  • Added test cases for case-insensitive matching: @Claude, @CLAUDE, @cLaUdE
  • Added test cases for hyphenated usernames: @claude-bot, @claude-mention
  • Added test case for multiline text
  • Verified email@claude.com and user@claude.com do NOT trigger

🤖 Generated with Claude Code

Yohei Demachi and others added 4 commits March 1, 2026 05:23
…ed mentions

The trigger phrase regex previously required mentions to be preceded by
whitespace or start-of-string, causing valid mentions like (@claude),
"@claude", and >@claude to silently fail. Expand the leading boundary
to include common punctuation chars (parentheses, quotes, angle brackets,
slashes) and add the case-insensitive flag so @claude also matches.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The FAQ previously stated @claude! wouldn't trigger, which is incorrect
after the regex boundary expansion. Updated to accurately describe
supported punctuation patterns and case-insensitive matching.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…ry matching

Replace the explicit character allowlist with `(?<!\w)` and `(?!\w)` word
boundary assertions. This categorically handles any non-word character
(colons, backticks, brackets, Markdown formatting, etc.) without needing
future allowlist expansions.

Also extract the duplicated regex construction into a shared
`buildTriggerRegex()` helper and add test cases for `cc:@claude`,
backtick-wrapped mentions, Markdown links, bold formatting, and
multiline text.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Treat hyphens as word-like characters in the lookbehind/lookahead
boundary so that hyphenated usernames like @claude-bot and
@claude-mention do not falsely trigger the action. This preserves the
existing behavior where only standalone trigger phrases match.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@yoheidemachi yoheidemachi changed the title fix: expand trigger_phrase regex to support punctuation-preceded mentions fix: use word-boundary lookbehind/lookahead for trigger phrase matching Mar 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant