Forum: Ruby on Rails Multiple Characters Negate Using Regexp

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
softwareengineer 99 (Guest)
on 2006-02-20 01:54
(Received via mailing list)
Dear experts,

  I am trying to build a regular expression to filter out anything
between <script ... > and </script> tags where I can  specify something
using negate class to exclude more than one character  in sequence.

  I tried:


  but obviously if the script has the character < before </script> then
my regexp breaks.

  I also tried:

  but it doesn't work

  Can anyone guide me on how I can specify multiple character negation?
  I will greatly appreciate it.

  Is this even possible? How else can I remove everything contained
between <script> tags?

softwareengineer 99 (Guest)
on 2006-02-20 02:16
(Received via mailing list)
Is it possible to specify a multiple line regular expression?

  Thanks for your assistance.
Jeff C. (Guest)
on 2006-02-20 05:06
Can you restate your original problem a bit better?  Are you wanting to
delete everything between the <script> and </script> tags?

If so, I think this expression should work (famous last words :-)

/(< *script.*?>).*?(<\/ *script *>)/

I tried it like this:

irb(main):001:0> "hello <script>goodbye</script> there".gsub(/(<
*script.*?>).*?(<\/ *script *>)/,'')
=> "hello  there"

Jamie M. (Guest)
on 2006-02-21 21:14
(Received via mailing list)
I recommend /<script([^>]+)>.*?<\/script>/

Adding the ? after the * makes it un-greedy, so it will match the
first following tag, as in the following example.

irb(main):001:0> s = "I have a <script foo> tag > inside < me
</script> don'cha know </script> :)"
=> "I have a <script foo> tag > inside < me </script> don'cha know
</script> :)"
irb(main):002:0> r = /<script([^>]+)>.*?<\/script>/
=> /<script([^>]+)>.*?<\/script>/
irb(main):003:0> s.gsub(r,'')
=> "I have a  don'cha know </script> :)"

- Jamie
Justin F. (Guest)
on 2006-02-24 14:37
(Received via mailing list)
softwareengineer 99 wrote:

> Is it possible to specify a multiple line regular expression?

yes, just put 'm' after it, e.g.





This topic is locked and can not be replied to.