# Regexp strip html

**URL:** <https://www.ruby-forum.com/t/regexp-strip-html/87283>\
**Category:** Ruby\
**Created:** [March 26, 2006, 4:34pm UTC](https://www.ruby-forum.com/t/regexp-strip-html/87283 "2006-03-26T16:34:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 26, 2006, 4:34pm UTC](https://www.ruby-forum.com/t/regexp-strip-html/87283/1 "2006-03-26T16:34:01Z")

</div>

i have a regexp able to strip html :

/\<[^\>]\*\>/

however, between \<script and all the "text is preserved, tjen  
i’ve tried :

def stripHTML

# self.gsub(/\<\S[^\>\<]\*\>/, ‘’)

```
self.gsub(/\A.*<body [^>]*>(.*)<\/body>\s*\Z/, '\1').gsub(/<[^>]*>/,

```

‘’)  
end

without success : the various javascript functions are kept ?

what’s my error here ?

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 27, 2006, 2:43pm UTC](https://www.ruby-forum.com/t/regexp-strip-html/87283/2 "2006-03-27T14:43:04Z")

</div>

On 26/03/06, Une bÃ©vue [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> i have a regexp able to strip html :
> 
> /\<[^\>]\*\>/
> 
> however, between \<script and all the "text is preserved, tjen  
> …  
> what’s my error here ?

Look at it this way: you have ‘’. You  
remove everything between angle brackets. You still have ‘Javascript’,  
because that’s not actually inside \<…\>.

The simplest solution is probably to do something like this before  
stripping out the remaining tags:

gsub(/\<script.\*?/im, ‘’)

Paul.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 27, 2006, 3:29pm UTC](https://www.ruby-forum.com/t/regexp-strip-html/87283/3 "2006-03-27T15:29:06Z")

</div>

Paul B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> The simplest solution is probably to do something like this  
> before  
> stripping out the remaining tags:

gsub(/\<script.\*?/im,

> ‘’)

yes, sounds clever ))
