str='this string has <i>html</i> code i want to <b>remove</b><br>Link Number 1 -><a href="http://www.bbc.co.uk">BBC</a> Link Number 1<br><p>Now back to normal text and stuff</p> '; str=str.replace(/<br>/gi, "\n"); str=str.replace(/<p.*>/gi, "\n"); str=str.replace(/<a.*href="(.*?)".*>(.*?)<\/a>/gi, " $2 (Link->$1) "); str=str.replace(/<(?:.|\s)*?>/g, "");
the str var starts out like this:-
this string has <i>html</i> code i want to <b>remove</b><br>Link Number 1 -><a href="http://www.bbc.co.uk">BBC</a> Link Number 1<br><p>Now back to normal text and stuff</p>
which renders like this:-
--start-- this string has <i>html</i> code i want to <b>remove</b><br>Link Number 1 -><a href="http://www.bbc.co.uk">BBC</a> Link Number 1<br><p>Now back to normal text and stuff</p> --end-- <br><br>
and then after the code has run it looks like this:-
this string has html code i want to remove Link Number 1 -> BBC (Link->http://www.bbc.co.uk) Link Number 1 Now back to normal text and stuff
As you can see the all the HTML has been removed and the Link have been persevered with the hyperlinked text is still intact. Also I have replaced the
and
tags with \n (newline char) so that some sort of visual formatting has been retained.
Read full article from jquery - Strip HTML from Text JavaScript - Stack Overflow
No comments:
Post a Comment