我的博客标题如下主要思想,关键术语,关键术语,关键术语
我希望主要思想和关键术语具有不同的字体大小。我想到的第一件事是搜索第一个逗号和字符串的末尾,并用相同的东西替换该 block ,但用带有类的 span 标签包围以使字体更小。
计划如下:
HTML(之前)
<a href="stupidreqexquestion">Main Idea, key term, key term, key term</a>
HTML(之后)
<a href="stupidreqexquestion">Main Idea <span class="smaller_font">, key term, key term key term</span></a>
我正在使用 Rails,所以我计划将其添加为辅助函数 - 例如:
helper
def make_key_words_in_title_smaller(title)
#replace the keywords in the title with key words surrounded by span tags
end
查看
<% @posts.each do |post |%>
<%= make_key_words_in_title_smaller(post.title)%>
<% end -%>
最佳答案
如果您不关心主要思想
部分是“欢迎回家,Roxy Carmichael”
,即双引号内的命令
>> t = "Main Idea, key term, key term, key term"
=> "Main Idea, key term, key term, key term"
>> t.gsub(/(.*?)(,.*)/, '\1 <span class="smaller_font">\2</span>')
=> "Main Idea <span class=\"smaller_font\">, key term, key term, key term</span>"
关于ruby-on-rails - 简单的正则 express 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3951015/