LoginSignup
0
0

More than 1 year has passed since last update.

【Ruby on Rails】gsubで特定の文字列を挟んでhtmlタグを表示させる方法

Last updated at Posted at 2022-08-03

僕がやろうとしていたこと

実務である特定の文字列が存在した場合に、その文字の色を赤くして表示ということをしたくなった。

その時に使用したので gsubというインスタンスメソッドで実現可能ということを知った。
このgsubを使用して、特定の文字が存在した場合にその文字を<span>タグを使って囲みstyleを適用することによって実現することができた。

どうやったか(コード例)

gorira = "僕は大人になったらゴリラになりたい"
gorira.gsub(/ゴリラ/, '<span style=color:red; font-weight:bold>\&</span>')   
# => "僕は大人になったら<sapn style=color:red; font-weight:bold>ゴリラ</span>になりたい"

こんな感じでやりたいことは一応実現することができた。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0