The problem is that regex syntax uses square brackets (to specify character classes), and TiddlyWiki filter syntax also uses square brackets (to specify literal text operands), and the filter parser does not allow nesting of brackets. Thus, when you use the character class regex syntax, it breaks the filter syntax due to nesting of square brackets.
The filter syntax makes it impossible to directly specify a regular expression that contains square brackets. The solution is to store the expression in a variable.
The workaround is to put your regex patterns into variables, and then use those variables in the filter, like this:
\define compare-by-last-name-with-character-class()
<$vars search="((?:.*\s)|^)([a-zA-Z0-9_]+$)" replace="$2, $1">
[<currentTiddler>search-replace:i:regexp<search>,<replace>]
</$vars>
\end
Note that is this particular use-case, it's not strictly necessary to put the "replace" pattern into a variable, since it doesn't actually use square brackets in the syntax. However, I find that putting both the search pattern and the replacement pattern into variables makes the filter syntax more consistent and easier to read.
enjoy,
-e