html to string

Solutions on MaxInterview for html to string by the best coders in the world

showing results for - "html to string"
Niclas
21 Aug 2020
1import { decode } from 'html-entities';
2
3function htmlToString(returnText: string) {
4  //-- remove BR tags and replace them with line break
5  returnText = returnText.replace(/<br>/gi, "\n");
6  returnText = returnText.replace(/<br\s\/>/gi, "\n");
7  returnText = returnText.replace(/<br\/>/gi, "\n");
8
9  // //-- remove P and A tags but preserve what's inside of them
10  returnText = returnText.replace(/<p.*>/gi, "\n");
11  returnText = returnText.replace(/<a.*href="(.*?)".*>(.*?)<\/a>/gi, " $2 ($1)");
12
13  // //-- remove all inside SCRIPT and STYLE tags
14  returnText = returnText.replace(/<script.*>[\w\W]{1,}(.*?)[\w\W]{1,}<\/script>/gi, "");
15  returnText = returnText.replace(/<style.*>[\w\W]{1,}(.*?)[\w\W]{1,}<\/style>/gi, "");
16
17  // //-- remove all else
18  returnText = returnText.replace(/<(?:.|\s)*?>/g, "");
19
20  // //-- get rid of more than 2 multiple line breaks:
21  returnText = returnText.replace(/(?:(?:\r\n|\r|\n)\s*){2,}/gim, "\n\n");
22
23  //-- get rid of more than 2 spaces:
24  returnText = returnText.replace(/ +(?= )/g, '');
25
26  returnText = returnText.replace(/<[^>]+>/g, '').replace(/&lt;/g, "<").replace(/&gt;/g, ">")
27  returnText = decode(returnText);
28  
29  return returnText;
30}
31
queries leading to this page
transform string in htmlhtml to string in javahtml code to string javascripthtml to string javascriptdocument html to stringcovert to string in htmlconvert htmlk to stringconverting html string to stringconvert html into string htmlconvert html page to stringconvert html tag to string javascriptjavascript html to stringhtml convert in stringhow to convert html to stringhtml lit to stringhtml to string converter functionhtml to string in jsconverting html tag to stringjs convert html to stringhtml to string html convertertransform html to stringhtml to stringconvert to a string in htmljavascript convert html into stringconvert string htmlconvert to string in htmlturn html value to stringconvert html value to stringcovert html to stringparse html data to stringstraight html to stringturn html into stringconvert into html stringconvert html code to stringhtml to string jshtml to stringgconvert html to string convert html to a string javascriptjavascript convert html to stringstring html to stringget html to string javascripthtml elemnt to stringto string in htmlhtml code convert into stringconvert html to js stringjs html to stringhtml code to stringconvert html to string in javascripthow to get html to stringhtml elemnt to string jsconvert code html to string html to string javaconvert html html to string jshtml to strto string w3convert code html to string javascripthtml to js stringconveter html to string javahtml string to stringconvert html tag to stringhow to convert html to string in javascriptconvert html dom to stringtag html to stringhow to convert html element to stringhtml as stringhtmlmetaelement to stringconvert html to stringhow to convert html into string in javascripthtml to stirnghow to convert html into stringhow to convert from html to stringconvert htmlto string javascriptconvert html code as an stringhtml convert into stringconvert html code to string javascriptconvert to html stringhtml to javascript stringcan i convert html to simple string html to string converterjavascript turn html to stringconvert html to string javascriptconverting html to stringconvert html code to string in javascripthow we convert html to string in javascripthow to convert html as string in javaconvert in to string in htmlconvert html to string and then convert string to htmlhtml to string htmlhtml value to stringconvert html to string jsconvert html into stringhtml to string