# File lib/bson/bson_ruby.rb, line 489
    def serialize_regex_element(buf, key, val)
      buf.put(REGEX)
      self.class.serialize_key(buf, key)

      str = val.source
      # We use serialize_key here since regex patterns aren't prefixed with
      # length (can't contain the NULL byte).
      self.class.serialize_key(buf, str)

      options = val.options
      options_str = ''
      options_str << 'i' if ((options & Regexp::IGNORECASE) != 0)
      if ((options & Regexp::MULTILINE) != 0)
        options_str << 'm'
        options_str << 's'
      end
      options_str << 'x' if ((options & Regexp::EXTENDED) != 0)
      options_str << val.extra_options_str if val.respond_to?(:extra_options_str)
      # Must store option chars in alphabetical order
      self.class.serialize_cstr(buf, options_str.split(//).sort.uniq.join)
    end