开发者

Rails - Regex help building a method that breaks long words with a <wbr> tag

Helo, I want to pass a block of html/text, could include links, into a method. The开发者_运维知识库 method needs to break words with more than 10 characters. Here's what I have so far:

def magic_format(str, len = 10)
 fragment = /.{#{len}}/
 str.split(/(\s+)/).map! { |word|
  (/\s/ === word) ? word : word.gsub(fragment, '\0<wbr />')
 }.join


 # AutoLink URLs
 str = auto_link(str, :html => { :target => '_blank' })

end

The gsub doesn't seem to be working for some reason, it isn't adding wbr tags.

Thoughts? Thanks


The map and regex part of that works (not sure if it's the nicest way of doing it, but it works). It's just not changing str. This should do the trick...

def magic_format(str, len = 10)
  fragment = /.{#{len}}/
  str = str.split(/(\s+)/).map! { |word|
    (/\s/ === word) ? word : word.gsub(fragment, '\0<wbr />')
  }.join

  auto_link(str, :html => { :target => '_blank' })
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜