js convert html to text 1

js convert html to text

// To remove all HTML tags use the ".replace(/<[^>]+>/g, '')" method.
let myHTML= "

Jimbo.

\n

That's what she said

"; let strippedHtml = myHTML.replace(/<[^>]+>/g, ''); console.log(stripedHtml); // Jimbo. That's what she said

Here is what the above code is Doing:
1. The first part of the regular expression, <, matches the opening angle bracket of an HTML tag. 2. The second part of the regular expression, [^>]+, matches any character that is not a closing angle bracket.
3. The third part of the regular expression, >, matches the closing angle bracket of an HTML tag.
4. The g flag tells JavaScript to perform a global match, meaning it will find all matches in the string, not just the first one.
5. The second argument to the replace method is the string to replace the matches with. In this case, we’re replacing the matches with an empty string, which effectively removes the HTML tags from the string.

Similar Posts