a <- "I hate extra spaces elephant"gsub("(?:\\G(?!^)|(?<!\\S)\\S{5})\\K\\S", ".", a, perl = TRUE)
Standard input is empty
[1] "I hate extra space. eleph..."
The brand new service which powers Ideone!
Widget for compiling and running the source code in a web browser!