GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

No menu could be recovered. Follow the blue links in the page.

This view

  • Original page
  • New address

GeezerVison is showing javascript.info — Find HTML comments

  • Tutorial
  • Regular expressions
  • Greedy and lazy quantifiers
back to the lesson

Find HTML comments

Find all HTML comments in the text:

let regexp = /your regexp/g;

let str = `... <!-- My -- comment
 test --> ..  <!----> ..
`;

alert( str.match(regexp) ); // '<!-- My -- comment \n test -->', '<!---->'

We need to find the beginning of the comment <!--, then everything till the end of -->.

An acceptable variant is <!--.*?--> – the lazy quantifier makes the dot stop right before -->. We also need to add flag s for the dot to include newlines.

Otherwise multiline comments won’t be found:

let regexp = /<!--.*?-->/gs;

let str = `... <!-- My -- comment
 test --> ..  <!----> ..
`;

alert( str.match(regexp) ); // '<!-- My -- comment \n test -->', '<!---->'
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.