Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

How to get the html comment at the end of the string (if there is any)

Last post 07-01-2009, 4:09 PM by ddrudik. 1 replies.
Sort Posts: Previous Next
  •  07-01-2009, 11:30 AM 54476

    How to get the html comment at the end of the string (if there is any)

    Hi, I have for example the following string:

    <!-- google_ad_section_start -->Some text bla bla!<!-- google_ad_section_end -->

    Now I need a regex to only find the last comment, but only if the string ends with a comment (maybe followed by some white spaces). If there is somebody asking for the sense of this, I need to add a read more link to the End of a teaser text. But sometimes there is after the text a comment (and sometimes also before), as you can see here: http://drupal.org/node/491536

    I thought this regex \s*<\!--.*?--\>\s*$ will do it but it returns the whole string between the first <!-- and the last-->. What can I do that the regex will only return the last comment (in the example: <!-- google_ad_section_end --> ) In this case: "<!-- google_ad_section_start -->Some text bla bla!<!-- google_ad_section_end --> some more text" The regex should return nothing, because the comment is not at the end of the string. Any ideas?

    Filed under: , ,
  •  07-01-2009, 4:09 PM 54480 in reply to 54476

    Re: How to get the html comment at the end of the string (if there is any)

    \s*<!--(?:(?!<!--|-->).)*?-->\s*$
View as RSS news feed in XML