Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Chao Lam http://blogs.coolchaser.com

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Regular expression to match an HTML comment

// description of your code here
This snippet uses the non-greedy matcher, and the "m" option to treat strings as multi-lines, so it may not work with all regex parsers.

   1  
   2  /\<!\s*--(.*?)(--\s*\>)/m
   3  
   4  Examples in Ruby IRB:
   5  irb(main):029:0> html = <<-EOL
   6  irb(main):030:0" <!--  First  Comment   --
   7  irb(main):031:0"       --> Second Comment <!--
   8  irb(main):032:0"       --  Third  Comment   -->
   9  irb(main):033:0" EOL
  10  => "<!--  First  Comment   --\n      --> Second Comment <!--\n      --  Third  Comment   -->\n"
  11  irb(main):075:0> m = html.match(/\<!\s*--(.*?)(--\s*\>)/m)
  12  => #<MatchData:0x15915a4>
  13  irb(main):076:0> m[0]
  14  => "<!--  First  Comment   --\n      -->"
  15  irb(main):077:0> m[1]
  16  => "  First  Comment   --\n      "
  17  
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS