[英]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. 我花了一段时间研究这个并且无法找到解决方案,我正在寻找一种从字符串中删除所有html标签的方法,如果出现img标签,那么只用src属性替换它们,对不起有什么小信息。 Thanks. 谢谢。
First do a regex replace, replacing this 首先进行正则表达式替换,替换它
<img.+?src="(.+?)".*?>
with this \\1
. 用这个\\1
。 That will replace the whole img tags with just the contents of the src attribute. 这将仅使用src属性的内容替换整个img标记。
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.