Read more

Ruby: How to keep split delimiter (separate, or as part of substrings)

Arne Hartherz
December 17, 2020Software engineer at makandra GmbH

Ruby's String#split returns an array of substrings from the given string. Usually, this is missing the split characters:

>> 'user@example.com'.split('@')
=> ["user", "example.com"]
Illustration UI/UX Design

UI/UX Design by makandra brand

We make sure that your target audience has the best possible experience with your digital product. You get:

  • Design tailored to your audience
  • Proven processes customized to your needs
  • An expert team of experienced designers
Read more Show archive.org snapshot

If you want to join those parts later on, you might know the split character and can just use it to join explicitly.
But if you split by a regular expression (for a set of split characters) that information is lost:

>> 'user@example.com'.split(/[@\.]/)
=> ["user", "example", "com"]

You can use a capture group to make those characters be part of the resulting array:

>> 'user@example.com'.split(/([@\.])/)
=> ["user", "@", "example", ".", "com"]

Or, you can use a capture group with look-behind to keep them with the resulting substrings:

>> 'user@example.com'.split(/(?<=[@\.])/)
=> ["user@", "example.", "com"]

An example use case for that is telling browsers to allow breaking a string after certain characters using wbr tags.

Posted by Arne Hartherz to makandra dev (2020-12-17 16:58)