Мастер-классы по Javascript Екатеринбург Ростов-на-Дону Москва Узнать больше...
Содержание (скрыть) Содержание (показать)

Find HTML comments

Find all HTML comments in the text:

str = '.. <!-- My -- comment \n test --> ..  <!----> .. '
re = /.. your regexp ../

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

Решение
Решение

We need to match everything .* from comment start <!-- to comment end -->. To let the comment end match, the quantifier should be lazy.

So, the first-glance solution for a task is <!--.*?-->.

But we remember that JavaScript dot doesn’t match a newline. So let’s replace it by a more generic [\s\S].

Finally:

str = '.. <!-- Welcome -- comment \n test --> ..  <!----> .. '
re = /<!--[\s\S]*?-->/g

alert( str.match(re) )

#289
Наверх

Реклама

Нашли опечатку?

Нашли опечатку на сайте? Что-то кажется странным?
Выделите соответствующий текст и нажмите Ctrl+Enter!

Последние Комментарии

Помоги другим!

Помоги другим узнать о хорошей статье!