Thực thể HTML thoát JavaScript

Bài viết này giải thích các giải pháp tốt nhất để Mã hóa các thực thể HTML trong JavaScript. Mã ví dụ bên dưới dịch bất kỳ chuỗi nào sang các thực thể HTML và quay lại nguyên mẫu chuỗi

Sử dụng /** * Conversion of string to HTML entities */ String.prototype.toHtmlEntities = function() { return this.replace(/./gm, function(s) { // return "&#" + s.charCodeAt(0) + ";"; return (s.match(/[a-z0-9\s]+/i)) ? s : "&#" + s.charCodeAt(0) + ";"; }); }; 3 để mã hóa chuỗi thành thực thể HTML

Trong mã JavaScript sau đây,

  • /**
     * Conversion of string to HTML entities
     */
    String.prototype.toHtmlEntities = function() {
        return this.replace(/./gm, function(s) {
            // return "&#" + s.charCodeAt(0) + ";";
            return (s.match(/[a-z0-9\s]+/i)) ? s : "&#" + s.charCodeAt(0) + ";";
        });
    };
    
    4 trả về một chuỗi chứa các thực thể HTML với sự trợ giúp của phương thức
    /**
     * Conversion of string to HTML entities
     */
    String.prototype.toHtmlEntities = function() {
        return this.replace(/./gm, function(s) {
            // return "&#" + s.charCodeAt(0) + ";";
            return (s.match(/[a-z0-9\s]+/i)) ? s : "&#" + s.charCodeAt(0) + ";";
        });
    };
    
    5
  • /**
     * Conversion of string to HTML entities
     */
    String.prototype.toHtmlEntities = function() {
        return this.replace(/./gm, function(s) {
            // return "&#" + s.charCodeAt(0) + ";";
            return (s.match(/[a-z0-9\s]+/i)) ? s : "&#" + s.charCodeAt(0) + ";";
        });
    };
    
    5 tìm giá trị của chuỗi đã cho và thay đổi nó bằng giá trị mong muốn/được chỉ định mới
  • /**
     * Creation of string from HTML entities
     */
    String.fromHtmlEntities = function(string) {
        return (string+"").replace(/&#\d+;/gm,function(s) {
            return String.fromCharCode(s.match(/\d+/gm)[0]);
        })
    };
    
    0 là một đối tượng trong JavaScript. Nó liên kết với các chức năng và đối tượng theo mặc định
  • /**
     * Creation of string from HTML entities
     */
    String.fromHtmlEntities = function(string) {
        return (string+"").replace(/&#\d+;/gm,function(s) {
            return String.fromCharCode(s.match(/\d+/gm)[0]);
        })
    };
    
    1 trả về Unicode của ký tự đã mô tả chỉ mục trong một chuỗi

/**
 * Conversion of string to HTML entities
 */
String.prototype.toHtmlEntities = function() {
    return this.replace(/./gm, function(s) {
        // return "&#" + s.charCodeAt(0) + ";";
        return (s.match(/[a-z0-9\s]+/i)) ? s : "&#" + s.charCodeAt(0) + ";";
    });
};

Sử dụng /** * Creation of string from HTML entities */ String.fromHtmlEntities = function(string) { return (string+"").replace(/&#\d+;/gm,function(s) { return String.fromCharCode(s.match(/\d+/gm)[0]); }) }; 2 để mã hóa chuỗi từ các thực thể HTML

Trong các câu lệnh JavaScript sau, chúng tôi đã tạo

  • /**
     * Creation of string from HTML entities
     */
    String.fromHtmlEntities = function(string) {
        return (string+"").replace(/&#\d+;/gm,function(s) {
            return String.fromCharCode(s.match(/\d+/gm)[0]);
        })
    };
    
    3 nhận bất kỳ
    /**
     * Creation of string from HTML entities
     */
    String.fromHtmlEntities = function(string) {
        return (string+"").replace(/&#\d+;/gm,function(s) {
            return String.fromCharCode(s.match(/\d+/gm)[0]);
        })
    };
    
    4 nào chứa các thực thể HTML
  • Ví dụ ở đây, giá trị chuỗi đã cho trong
    /**
     * Conversion of string to HTML entities
     */
    String.prototype.toHtmlEntities = function() {
        return this.replace(/./gm, function(s) {
            // return "&#" + s.charCodeAt(0) + ";";
            return (s.match(/[a-z0-9\s]+/i)) ? s : "&#" + s.charCodeAt(0) + ";";
        });
    };
    
    5 là
    /**
     * Creation of string from HTML entities
     */
    String.fromHtmlEntities = function(string) {
        return (string+"").replace(/&#\d+;/gm,function(s) {
            return String.fromCharCode(s.match(/\d+/gm)[0]);
        })
    };
    
    6 được thay thế bằng
    /**
     * Creation of string from HTML entities
     */
    String.fromHtmlEntities = function(string) {
        return (string+"").replace(/&#\d+;/gm,function(s) {
            return String.fromCharCode(s.match(/\d+/gm)[0]);
        })
    };
    
    7 trả về chuỗi
    /**
     * Creation of string from HTML entities
     */
    String.fromHtmlEntities = function(string) {
        return (string+"").replace(/&#\d+;/gm,function(s) {
            return String.fromCharCode(s.match(/\d+/gm)[0]);
        })
    };
    
    8
  • /**
     * Creation of string from HTML entities
     */
    String.fromHtmlEntities = function(string) {
        return (string+"").replace(/&#\d+;/gm,function(s) {
            return String.fromCharCode(s.match(/\d+/gm)[0]);
        })
    };
    
    8 là một phương thức trả về một chuỗi được tạo bởi một chuỗi

/**
 * Creation of string from HTML entities
 */
String.fromHtmlEntities = function(string) {
    return (string+"").replace(/&#\d+;/gm,function(s) {
        return String.fromCharCode(s.match(/\d+/gm)[0]);
    })
};

Sau đó, sử dụng chức năng như dưới đây

  • Đây là một biến
    /**
     * Creation of string from HTML entities
     */
    String.fromHtmlEntities = function(string) {
        return (string+"").replace(/&#\d+;/gm,function(s) {
            return String.fromCharCode(s.match(/\d+/gm)[0]);
        })
    };
    
    10 được khởi tạo với một chuỗi chứa các ký tự đặc biệt (
    /**
     * Creation of string from HTML entities
     */
    String.fromHtmlEntities = function(string) {
        return (string+"").replace(/&#\d+;/gm,function(s) {
            return String.fromCharCode(s.match(/\d+/gm)[0]);
        })
    };
    
    11)
  • Hàm
    /**
     * Creation of string from HTML entities
     */
    String.fromHtmlEntities = function(string) {
        return (string+"").replace(/&#\d+;/gm,function(s) {
            return String.fromCharCode(s.match(/\d+/gm)[0]);
        })
    };
    
    12 được gọi với chuỗi ký tự đặc biệt này
    /**
     * Creation of string from HTML entities
     */
    String.fromHtmlEntities = function(string) {
        return (string+"").replace(/&#\d+;/gm,function(s) {
            return String.fromCharCode(s.match(/\d+/gm)[0]);
        })
    };
    
    10
  • /**
     * Creation of string from HTML entities
     */
    String.fromHtmlEntities = function(string) {
        return (string+"").replace(/&#\d+;/gm,function(s) {
            return String.fromCharCode(s.match(/\d+/gm)[0]);
        })
    };
    
    14 là một chức năng mặc định của JavaScript hiển thị các giá trị trong hộp nhật ký

/**
 * Creation of string from HTML entities
 */
String.fromHtmlEntities = function(string) {
    return (string+"").replace(/&#\d+;/gm,function(s) {
        return String.fromCharCode(s.match(/\d+/gm)[0]);
    })
};
1

Đầu ra từ bàn điều khiển như sau

/**
 * Creation of string from HTML entities
 */
String.fromHtmlEntities = function(string) {
    return (string+"").replace(/&#\d+;/gm,function(s) {
        return String.fromCharCode(s.match(/\d+/gm)[0]);
    })
};
7

Sử dụng hàm /** * Creation of string from HTML entities */ String.fromHtmlEntities = function(string) { return (string+"").replace(/&#\d+;/gm,function(s) { return String.fromCharCode(s.match(/\d+/gm)[0]); }) }; 15 để mã hóa các thực thể HTML trong JavaScript

Bạn có thể kiểm tra các chức năng này trên trình biên dịch JavaScript trực tuyến hoặc ngoại tuyến. Nó giúp bạn hiển thị

/**
 * Creation of string from HTML entities
 */
String.fromHtmlEntities = function(string) {
    return (string+"").replace(/&#\d+;/gm,function(s) {
        return String.fromCharCode(s.match(/\d+/gm)[0]);
    })
};
4 mà không cần trình duyệt đọc HTML của nó. Điều này có thể được thực hiện theo cách khác. Bạn có thể tạo một đối tượng gồm các thực thể HTML

  • Một biến
    /**
     * Creation of string from HTML entities
     */
    String.fromHtmlEntities = function(string) {
        return (string+"").replace(/&#\d+;/gm,function(s) {
            return String.fromCharCode(s.match(/\d+/gm)[0]);
        })
    };
    
    17 chứa đối tượng gồm các ký tự và ký hiệu đặc biệt
  • Hàm
    /**
     * Creation of string from HTML entities
     */
    String.fromHtmlEntities = function(string) {
        return (string+"").replace(/&#\d+;/gm,function(s) {
            return String.fromCharCode(s.match(/\d+/gm)[0]);
        })
    };
    
    18 nhận chuỗi và trả về giá trị với sự trợ giúp của điều kiện
    /**
     * Conversion of string to HTML entities
     */
    String.prototype.toHtmlEntities = function() {
        return this.replace(/./gm, function(s) {
            // return "&#" + s.charCodeAt(0) + ";";
            return (s.match(/[a-z0-9\s]+/i)) ? s : "&#" + s.charCodeAt(0) + ";";
        });
    };
    
    5

/**
 * Conversion of string to HTML entities
 */
String.prototype.toHtmlEntities = function() {
    return this.replace(/./gm, function(s) {
        // return "&#" + s.charCodeAt(0) + ";";
        return (s.match(/[a-z0-9\s]+/i)) ? s : "&#" + s.charCodeAt(0) + ";";
    });
};
2

Không có bất kỳ phiên bản gốc nào của các thực thể HTML trong JavaScript. Nếu bạn chỉ cần những kiến ​​thức cơ bản để trình duyệt không hiểu là HTML. PHP. js, là nhiệm vụ chuyển từng khả năng cục bộ của PHP sang JavaScript, cũng chứa một mô hình. Dọc theo những dòng này, tôi đã giữ cho nó đơn giản và sử dụng bên dưới

Làm cách nào để thoát khỏi các thực thể HTML trong JavaScript?

Làm cách nào tôi có thể thoát các ký tự đặc biệt HTML trong JavaScript? .
Sử dụng Phương thức createTextNode()
Sử dụng thuộc tính textContent
Sử dụng phương thức thay thế()

Làm cách nào để xử lý các thực thể HTML trong JavaScript?

chỉ cần Gọi một Hàm với một đối số. .
Giải mã hàm thực thể HTML decodeHTMLEntities(text) { var textArea = document. .
Decode HTML-entities (JQuery) function decodeHTMLEntities(text) { return $("