I've encountered a wierd situation when trying to match the first character of a string using a regex with alternatives. It seems that replacement stops when matching a single character using a period at the start of the string, but not for slightly different patterns.
Desired behavior:
"foo bar" -> "Foo Bar"
(replace-regexp-in-string
"^.\\| ."
#'upcase
"foo bar")
> "Foo bar"
Notice that replacement stops and doesn't capitalize the 'b' in 'bar'. However:
(replace-regexp-in-string
"^f\\| ."
#'upcase
"foo bar")
> "Foo Bar"
(replace-regexp-in-string
"^.o\\| ."
#'upcase
"foo bar")
> "FOo Bar"
(replace-regexp-in-string
"^..\\| ."
#'upcase
"foo bar")
> "FOo Bar"
I have tried parenthesizing all combinations of the alternatives to no effect.
I'm not concerned about beginning of string vs beginning of line ('\`' vs '^'), but FWIW this odd behavior is consistent with both patterns.
I'm observing this behavior with both emacs 24.5.1 and 25.0.95.1.
M-x report-emacs-bug
?capitalize
, notreplace-regexp-in-string
:(capitalize "foo bar") => "Foo Bar"
"\\<."
may be the regexp you want. (That is: beginning of word followed by character).(replace-regexp-in-string "\\<." #'upcase "foo bar") => "Foo Bar"