简体   繁体   中英

JS Regex to replace all html tags

i have spent a while researching this and cant find a solution, i am looking for a method to remove all html tags from a string and if img tags appear, then replace them with just the src attribute, sorry for little information. Thanks.

First do a regex replace, replacing this

<img.+?src="(.+?)".*?>

with this \\1 . That will replace the whole img tags with just the contents of the src attribute.

Then do the replacement for all the rest of the tags using this

<.+?>

and replace with nothing/blank.

I think something like this might be what you need:

 var str = "<b>TEST</b> <p><img src='path.png' /> boom <span>sss</span></p>"; var text = $(str).find('img').replaceWith(function() { return $(this).attr('src'); }).end().text(); alert(text); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM