I am writing a code to extract all the plain content from the tag of the HTML code.I know it can be done using the document element. But I need to do this using REGEX only I have written the following code, but it has some bugs which I am not able to figure out on how to solve it.
function htmlToText(html) {
return html.
replace(/(.|\n)*<body.*>/, ''). //remove up till body
replace(/<\/body(.|\n)*/, ''). //remove from </body
replace(/<.+\>/, ''). //remove tags
replace(/^\s\n*$/gm, ''); //remove empty lines
}
Here is the solution for it
function htmlToText(html) {
return html.
replace(/(.|\n)*<body.*>/, ''). //remove up till body
replace(/<\/body(.|\n)*/g, ''). //remove from </body
replace(/<.+\>/g, ''). //remove tags
replace(/^\s\n*$/gm, ''); //remove empty lines
}