Hướng dẫn how to remove all html tags from a string js? - làm thế nào để loại bỏ tất cả các thẻ html khỏi một chuỗi js?

Tôi đã thay đổi câu trả lời của JibberBoy2000 để bao gồm một số định dạng thẻ <BR />, xóa mọi thứ bên trong các thẻ <SCRIPT><STYLE>, định dạng HTML kết quả bằng cách loại bỏ nhiều lỗi và khoảng trắng và chuyển đổi một số mã được mã hóa HTML thành bình thường. Sau một số thử nghiệm, có vẻ như bạn có thể chuyển đổi hầu hết các trang web đầy đủ thành văn bản đơn giản nơi giữ lại tiêu đề và nội dung trang.

Trong ví dụ đơn giản,

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<!--comment-->

<head>

<title>This is my title</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style>

    body {margin-top: 15px;}
    a { color: #D80C1F; font-weight:bold; text-decoration:none; }

</style>
</head>

<body>
    <center>
        This string has <i>html</i> code i want to <b>remove</b><br>
        In this line <a href="http://www.bbc.co.uk">BBC</a> with link is mentioned.<br/>Now back to &quot;normal text&quot; and stuff using &lt;html encoding&gt;                 
    </center>
</body>
</html>

trở thành

Đây là tiêu đề của tôi

Chuỗi này có mã html tôi muốn xóa

Trong dòng này BBC (http://www.bbc.co.uk) với liên kết được đề cập.

Bây giờ trở lại "văn bản bình thường" và các công cụ sử dụng

Chức năng JavaScript và trang kiểm tra xem xét điều này:

function convertHtmlToText() {
    var inputText = document.getElementById("input").value;
    var returnText = "" + inputText;

    //-- remove BR tags and replace them with line break
    returnText=returnText.replace(/<br>/gi, "\n");
    returnText=returnText.replace(/<br\s\/>/gi, "\n");
    returnText=returnText.replace(/<br\/>/gi, "\n");

    //-- remove P and A tags but preserve what's inside of them
    returnText=returnText.replace(/<p.*>/gi, "\n");
    returnText=returnText.replace(/<a.*href="(.*?)".*>(.*?)<\/a>/gi, " $2 ($1)");

    //-- remove all inside SCRIPT and STYLE tags
    returnText=returnText.replace(/<script.*>[\w\W]{1,}(.*?)[\w\W]{1,}<\/script>/gi, "");
    returnText=returnText.replace(/<style.*>[\w\W]{1,}(.*?)[\w\W]{1,}<\/style>/gi, "");
    //-- remove all else
    returnText=returnText.replace(/<(?:.|\s)*?>/g, "");

    //-- get rid of more than 2 multiple line breaks:
    returnText=returnText.replace(/(?:(?:\r\n|\r|\n)\s*){2,}/gim, "\n\n");

    //-- get rid of more than 2 spaces:
    returnText = returnText.replace(/ +(?= )/g,'');

    //-- get rid of html-encoded characters:
    returnText=returnText.replace(/&nbsp;/gi," ");
    returnText=returnText.replace(/&amp;/gi,"&");
    returnText=returnText.replace(/&quot;/gi,'"');
    returnText=returnText.replace(/&lt;/gi,'<');
    returnText=returnText.replace(/&gt;/gi,'>');

    //-- return
    document.getElementById("output").value = returnText;
}

Nó đã được sử dụng với HTML này:

<textarea id="input" style="width: 400px; height: 300px;"></textarea><br />
<button onclick="convertHtmlToText()">CONVERT</button><br />
<textarea id="output" style="width: 400px; height: 300px;"></textarea><br />

Xuất bản ngày 27 tháng 4 năm 2021

Để xóa tất cả các thẻ HTML khỏi chuỗi, chúng ta có thể sử dụng phương thức chuỗi replace() trong JavaScript.

TL;DR

// string with html tags
const str = "<div><h2>Hello World!</h2></div>";

// remove all html tags from string
str.replace(/(<([^>]+)>)/gi, ""); // Hello World

Ví dụ: hãy nói rằng chúng ta có một chuỗi với một số thẻ HTML trong chúng như thế này,

// string with html tags
const str = "<div><h2>Hello World!</h2></div>";

Mục tiêu của chúng tôi là xóa các thẻ

function convertHtmlToText() {
    var inputText = document.getElementById("input").value;
    var returnText = "" + inputText;

    //-- remove BR tags and replace them with line break
    returnText=returnText.replace(/<br>/gi, "\n");
    returnText=returnText.replace(/<br\s\/>/gi, "\n");
    returnText=returnText.replace(/<br\/>/gi, "\n");

    //-- remove P and A tags but preserve what's inside of them
    returnText=returnText.replace(/<p.*>/gi, "\n");
    returnText=returnText.replace(/<a.*href="(.*?)".*>(.*?)<\/a>/gi, " $2 ($1)");

    //-- remove all inside SCRIPT and STYLE tags
    returnText=returnText.replace(/<script.*>[\w\W]{1,}(.*?)[\w\W]{1,}<\/script>/gi, "");
    returnText=returnText.replace(/<style.*>[\w\W]{1,}(.*?)[\w\W]{1,}<\/style>/gi, "");
    //-- remove all else
    returnText=returnText.replace(/<(?:.|\s)*?>/g, "");

    //-- get rid of more than 2 multiple line breaks:
    returnText=returnText.replace(/(?:(?:\r\n|\r|\n)\s*){2,}/gim, "\n\n");

    //-- get rid of more than 2 spaces:
    returnText = returnText.replace(/ +(?= )/g,'');

    //-- get rid of html-encoded characters:
    returnText=returnText.replace(/&nbsp;/gi," ");
    returnText=returnText.replace(/&amp;/gi,"&");
    returnText=returnText.replace(/&quot;/gi,'"');
    returnText=returnText.replace(/&lt;/gi,'<');
    returnText=returnText.replace(/&gt;/gi,'>');

    //-- return
    document.getElementById("output").value = returnText;
}
0,
function convertHtmlToText() {
    var inputText = document.getElementById("input").value;
    var returnText = "" + inputText;

    //-- remove BR tags and replace them with line break
    returnText=returnText.replace(/<br>/gi, "\n");
    returnText=returnText.replace(/<br\s\/>/gi, "\n");
    returnText=returnText.replace(/<br\/>/gi, "\n");

    //-- remove P and A tags but preserve what's inside of them
    returnText=returnText.replace(/<p.*>/gi, "\n");
    returnText=returnText.replace(/<a.*href="(.*?)".*>(.*?)<\/a>/gi, " $2 ($1)");

    //-- remove all inside SCRIPT and STYLE tags
    returnText=returnText.replace(/<script.*>[\w\W]{1,}(.*?)[\w\W]{1,}<\/script>/gi, "");
    returnText=returnText.replace(/<style.*>[\w\W]{1,}(.*?)[\w\W]{1,}<\/style>/gi, "");
    //-- remove all else
    returnText=returnText.replace(/<(?:.|\s)*?>/g, "");

    //-- get rid of more than 2 multiple line breaks:
    returnText=returnText.replace(/(?:(?:\r\n|\r|\n)\s*){2,}/gim, "\n\n");

    //-- get rid of more than 2 spaces:
    returnText = returnText.replace(/ +(?= )/g,'');

    //-- get rid of html-encoded characters:
    returnText=returnText.replace(/&nbsp;/gi," ");
    returnText=returnText.replace(/&amp;/gi,"&");
    returnText=returnText.replace(/&quot;/gi,'"');
    returnText=returnText.replace(/&lt;/gi,'<');
    returnText=returnText.replace(/&gt;/gi,'>');

    //-- return
    document.getElementById("output").value = returnText;
}
1, v.v. Vì vậy, hãy sử dụng phương thức chuỗi replace() và vượt qua:

  • Một biểu thức regex của
    function convertHtmlToText() {
        var inputText = document.getElementById("input").value;
        var returnText = "" + inputText;
    
        //-- remove BR tags and replace them with line break
        returnText=returnText.replace(/<br>/gi, "\n");
        returnText=returnText.replace(/<br\s\/>/gi, "\n");
        returnText=returnText.replace(/<br\/>/gi, "\n");
    
        //-- remove P and A tags but preserve what's inside of them
        returnText=returnText.replace(/<p.*>/gi, "\n");
        returnText=returnText.replace(/<a.*href="(.*?)".*>(.*?)<\/a>/gi, " $2 ($1)");
    
        //-- remove all inside SCRIPT and STYLE tags
        returnText=returnText.replace(/<script.*>[\w\W]{1,}(.*?)[\w\W]{1,}<\/script>/gi, "");
        returnText=returnText.replace(/<style.*>[\w\W]{1,}(.*?)[\w\W]{1,}<\/style>/gi, "");
        //-- remove all else
        returnText=returnText.replace(/<(?:.|\s)*?>/g, "");
    
        //-- get rid of more than 2 multiple line breaks:
        returnText=returnText.replace(/(?:(?:\r\n|\r|\n)\s*){2,}/gim, "\n\n");
    
        //-- get rid of more than 2 spaces:
        returnText = returnText.replace(/ +(?= )/g,'');
    
        //-- get rid of html-encoded characters:
        returnText=returnText.replace(/&nbsp;/gi," ");
        returnText=returnText.replace(/&amp;/gi,"&");
        returnText=returnText.replace(/&quot;/gi,'"');
        returnText=returnText.replace(/&lt;/gi,'<');
        returnText=returnText.replace(/&gt;/gi,'>');
    
        //-- return
        document.getElementById("output").value = returnText;
    }
    
    3 là đối số đầu tiên sẽ xóa tất cả các thẻ như
    function convertHtmlToText() {
        var inputText = document.getElementById("input").value;
        var returnText = "" + inputText;
    
        //-- remove BR tags and replace them with line break
        returnText=returnText.replace(/<br>/gi, "\n");
        returnText=returnText.replace(/<br\s\/>/gi, "\n");
        returnText=returnText.replace(/<br\/>/gi, "\n");
    
        //-- remove P and A tags but preserve what's inside of them
        returnText=returnText.replace(/<p.*>/gi, "\n");
        returnText=returnText.replace(/<a.*href="(.*?)".*>(.*?)<\/a>/gi, " $2 ($1)");
    
        //-- remove all inside SCRIPT and STYLE tags
        returnText=returnText.replace(/<script.*>[\w\W]{1,}(.*?)[\w\W]{1,}<\/script>/gi, "");
        returnText=returnText.replace(/<style.*>[\w\W]{1,}(.*?)[\w\W]{1,}<\/style>/gi, "");
        //-- remove all else
        returnText=returnText.replace(/<(?:.|\s)*?>/g, "");
    
        //-- get rid of more than 2 multiple line breaks:
        returnText=returnText.replace(/(?:(?:\r\n|\r|\n)\s*){2,}/gim, "\n\n");
    
        //-- get rid of more than 2 spaces:
        returnText = returnText.replace(/ +(?= )/g,'');
    
        //-- get rid of html-encoded characters:
        returnText=returnText.replace(/&nbsp;/gi," ");
        returnText=returnText.replace(/&amp;/gi,"&");
        returnText=returnText.replace(/&quot;/gi,'"');
        returnText=returnText.replace(/&lt;/gi,'<');
        returnText=returnText.replace(/&gt;/gi,'>');
    
        //-- return
        document.getElementById("output").value = returnText;
    }
    
    4 và
    function convertHtmlToText() {
        var inputText = document.getElementById("input").value;
        var returnText = "" + inputText;
    
        //-- remove BR tags and replace them with line break
        returnText=returnText.replace(/<br>/gi, "\n");
        returnText=returnText.replace(/<br\s\/>/gi, "\n");
        returnText=returnText.replace(/<br\/>/gi, "\n");
    
        //-- remove P and A tags but preserve what's inside of them
        returnText=returnText.replace(/<p.*>/gi, "\n");
        returnText=returnText.replace(/<a.*href="(.*?)".*>(.*?)<\/a>/gi, " $2 ($1)");
    
        //-- remove all inside SCRIPT and STYLE tags
        returnText=returnText.replace(/<script.*>[\w\W]{1,}(.*?)[\w\W]{1,}<\/script>/gi, "");
        returnText=returnText.replace(/<style.*>[\w\W]{1,}(.*?)[\w\W]{1,}<\/style>/gi, "");
        //-- remove all else
        returnText=returnText.replace(/<(?:.|\s)*?>/g, "");
    
        //-- get rid of more than 2 multiple line breaks:
        returnText=returnText.replace(/(?:(?:\r\n|\r|\n)\s*){2,}/gim, "\n\n");
    
        //-- get rid of more than 2 spaces:
        returnText = returnText.replace(/ +(?= )/g,'');
    
        //-- get rid of html-encoded characters:
        returnText=returnText.replace(/&nbsp;/gi," ");
        returnText=returnText.replace(/&amp;/gi,"&");
        returnText=returnText.replace(/&quot;/gi,'"');
        returnText=returnText.replace(/&lt;/gi,'<');
        returnText=returnText.replace(/&gt;/gi,'>');
    
        //-- return
        document.getElementById("output").value = returnText;
    }
    
    5 và văn bản bên trong nó.
  • và một chuỗi trống là đối số thứ hai để nói rằng chúng ta chỉ muốn thay thế các ký tự phù hợp bằng một chuỗi trống.

Nó có thể được thực hiện như thế này,

// string with html tags
const str = "<div><h2>Hello World!</h2></div>";

// remove all html tags from string
const strWithoutHTmlTags = str.replace(/(<([^>]+)>)/gi, "");

console.log(strWithoutHTmlTags); // Hello World!

Và chúng tôi đã xóa thành công tất cả các thẻ HTML khỏi chuỗi. 😃

Xem mã trên trực tiếp trong JSBIN

Hãy chia sẻ nếu bạn thấy điều này hữu ích.


Chức năng nào được sử dụng để xóa tất cả các thẻ HTML khỏi chuỗi?

Chức năng dải_tags () dải một chuỗi từ các thẻ HTML, XML và PHP. Lưu ý: Nhận xét HTML luôn bị tước.strip_tags() function strips a string from HTML, XML, and PHP tags. Note: HTML comments are always stripped.

JavaScript có thể loại bỏ HTML không?

Cho một phần tử HTML và tác vụ là xóa phần tử HTML khỏi tài liệu bằng JavaScript.Cách tiếp cận: Chọn phần tử HTML cần loại bỏ.Sử dụng phương thức JavaScript Remove () và removeChild () để xóa phần tử khỏi tài liệu HTML.Use JavaScript remove() and removeChild() method to remove the element from the HTML document.

Làm cách nào để loại bỏ thẻ HTML?

Các thẻ HTML có thể được xóa khỏi một chuỗi đã cho bằng cách sử dụng phương thức thay thế () của lớp chuỗi.Chúng ta có thể xóa các thẻ HTML khỏi một chuỗi đã cho bằng cách sử dụng biểu thức thông thường.Sau khi xóa các thẻ HTML khỏi một chuỗi, nó sẽ trả về một chuỗi dưới dạng văn bản thông thường.by using replaceAll() method of String class. We can remove the HTML tags from a given string by using a regular expression. After removing the HTML tags from a string, it will return a string as normal text.

Có thể xóa các thẻ HTML khỏi dữ liệu không?

PHP cung cấp một chức năng sẵn có để xóa các thẻ HTML khỏi dữ liệu.Chức năng Strip_tags () là một hàm sẵn có trong PHP loại bỏ các chuỗi HTML, XML và PHP.Nó chấp nhận hai tham số.Hàm này trả về một chuỗi với tất cả các thẻ null byte, HTML và PHP bị tước từ một $ str.. The strip_tags() function is an inbuilt function in PHP that removes the strings form HTML, XML and PHP tags. It accepts two parameters. This function returns a string with all NULL bytes, HTML, and PHP tags stripped from a given $str.