开发者

Eliminate html tags

开发者 https://www.devze.com 2023-02-08 06:35 出处:网络
I have a html string like Lorem ipsum <a href=\'google.com\'>google <span>rulz</span></a> not sum

I have a html string like

Lorem ipsum <a href='google.com'>google <span>rulz</span></a> not sum

I wanna delete all html tags, I mean I wanna delete all the html elements, so the string should transform to (in javascript)

Lorem ipsum not sum

I tried it matchin开发者_高级运维g (and replace) it ussing:

<([a-z][a-z0-9]*)\b[^>]*>.+?<\/([a-z][a-z0-9]*)\b[^>]*>

The problem is that I get

Lorem ipsum </a> not sum

I suposse the it happens because the regular expression replace "google rulz" but I would like it to replace the hole html tag.

Any idea?


you can use jQuery to create a virtual element and get it's text contents, i.e.


<script type='text/javascript'>
d = $('div');
d.html(text);
stripped = d.text();
</script>


You could use the php.js strip_tags() function.


If you are retrieving this text from an element you could get its innerText which will return the text with no html like so.

console.log(getElementById('someId').innerText);


<[^>]*> matches anything that looks remotely like a HTML tag. If this is not good enough, you should use a full fledged HTML parser.


try var tags = /<\/?([a-z][a-z0-9]*)\b[^>]*>/gi

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号