how could i skip or replace every other character (could be anything) with regex?
"abc123.-def45".gsub(/.(.)?/, '@')
to get
"a@c@2@.@d@f@5"
Capture the first character instead, and write it back:
"abc123.-def45".gsub(/(.)./, '\1@')
It's important that you don't make the second character optional. Otherwise, in an odd-length string, the last character would lead to a match, and a @ would be appended. Without the ?, the last character will simply fail and remain untouched.
Working demo.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With