0

What I am trying to achieve here is to fetch the HTML within a rich text editor and I need to uncomment all the img tags which are commented within that HTML. I don't want any other HTML code to get uncomment. For example

Input - HTML of rich text having commented tags

Output - Only img tag should get uncomment

I am having the regex for two conditions :- 1.) To uncomment all the HTML <!--(.*?)--> 2.) To get IMG tag alone from the HTML - <img([\w\W]+?)/>

Its not working when I am combining both the REGEX to get my required result. Please suggest.

1

I think the regex you're looking for is <!--\s*(<img[\w\W]+?>)\s*-->.

I run the following script:

$html = '<!-- <img src="-/media/67a6sahgshghs6as76.ashx" alt=""  width="100%" height="auto" class="figure"> -->';
$regex = '<!--\s*(<img[\w\W]+?>)\s*-->';
$matches = ([regex]$regex).Matches($html)
$matches.Groups[1].Value

And the output is

<img src="-/media/67a6sahgshghs6as76.ashx" alt=""  width="100%" height="auto" class="figure">

Remember that parsing html with regex is not recommended. You can try to use a library like HtmlAgilityPack. And that the script above only works when <img tag is the only content in the html comment.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.