Regex: select everything except img tag

I am trying to select text using regular expressions, leaving the tags imgintact.

I found the following code that selects all tags img:

/<img[^>]+>/g

but actually has the following text:

This is an untagged text.
<p>this is my paragraph text</p>
<img src="http://example.com/image.png" alt=""/>
<a href="http://example.com/">this is a link</a>

using the code above, select the img tag only

/<img[^>]+>/g #--> using this code will result in:
<img src="http://example.com/image.png" alt=""/>

but I would like to use some regex that selects everything except the image:

/magical regex/g # --> results in:
This is an untagged text.
<p>this is my paragraph text</p>
<a href="http://example.com/">this is a link</a>

I also found this code:

/<(?!img)[^>]+>/g

which selects everything tagsexcept img. but in some cases I will have unlabeled text or text between tags, so this will not work for my case .:(

? , , , , .


UPDATE:

, , , , , , .

, - , Yahoo Pipes, . .

, , regex yahoo:

http://pipes.yahoo.com/pipes/docs?doc=operators#Regex


2

img, , @Blixt, :

<(?!img)[^>]+> , replace with "" #-> strips out every tag that is not img
(?s)^[^<]*(.*), replace with $1  #-> removes all the text before the img tag
(?s)^([^>]+>).*, replace with $1 #-> removed all the text after the img tag

, img, - , , .

+2
2

, , .

, PHP:

$htmlWithoutIMG = preg_replace('/<img[^>]+>/g', '', $html);

Javascript:

var htmlWithoutIMG = html.replace(/<img[^>]+>/g, '');

, <img> , .. , , . , <,>.

+1

. , , , ( , , ).

, <img>, <img> <img> . ( ).

, . , <img> , .

0

Source: https://habr.com/ru/post/1778596/


All Articles