Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想要做的是找到所有由下划线分隔的字母字符序列的单词,例如:
L_L_L
并将它们变成如下标签:
<acronym>L L L</acronym>
首字母缩略词可以是任意长度,因此我正在努力寻找所有实例。
假设您的示例首字母缩写词代表“任何由下划线分隔的英文大写字母序列”,您可以执行以下操作:
def replace_acronyms(str) str.gsub(/\b([A-Z]+(?:_[A-Z]+)+)\b/) do |m,g1| '<acronym>' + $1.gsub(/_/,' ') + '</acronym>' end end replace_acronyms('Foo L_L_L bar.') # => "Foo <acronym>L L L</acronym> bar."