Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove contents within a specific tag

Using Rails 3.2. I want to remove all text in <b> and the tags, but I manage to find ways to strip the tags only.:

string = "
  <p>
    <b>Section 1</b>
    Everything is good.<br>
    <b>Section 2</b>
    All is well.
  </p>"
string.strip_tags
# => "Section 1 Everthing is good. Section 2 All is well."

I want to achieve this:

"Everthing is good. All is well."

Should I add regex matching too?

like image 729
Victor Avatar asked Feb 14 '23 20:02

Victor


1 Answers

The "right" way would be to use an html parser like Nokogiri.
However for this simple task, you may use a regex. It's quite simple:
Search for : (?m)<b\s*>.*?<\/b\s*> and replace it with empty string. After that, use strip_tags.

Regex explanation:

(?m)    # set the m modifier to match newlines with dots .
<b      # match <b
\s*     # match a whitespace zero or more times
>       # match >
.*?     # match anything ungreedy until </b found
<\/b    # match </b
\s*     # match a whitespace zero or more times
>       # match >

Online demo

like image 147
HamZa Avatar answered Feb 17 '23 20:02

HamZa