Unescape thoát HTML

Bắt đầu nội dung chính

OSCC · JavaScript, Chuỗi, Trình duyệt, Regexp · 22 tháng 10, 2020

Unescape thoát HTML

Các ký tự HTML đã thoát Unescapes

  • Sử dụng
    Geeks for Geeks!!!
    To contribute articles contact us at  
    [email protected]
    4 với biểu thức chính quy khớp với các ký tự cần thoát
  • Sử dụng lệnh gọi lại của hàm để thay thế từng phiên bản ký tự đã thoát bằng ký tự chưa thoát được liên kết của nó bằng cách sử dụng từ điển (đối tượng)

const unescapeHTML = str =>
  str.replace(
    /&|<|>|'|"/g,
    tag =>
      ({
        '&': '&',
        '<': '<',
        '>': '>',
        ''': "'",
        '"': '"'
      }[tag] || tag)
  );

Đoạn trích được đề xuất

  • Thoát một chuỗi để sử dụng trong HTML

  • Chuyển đổi một chuỗi màu

    Geeks for Geeks!!!
    To contribute articles contact us at  
    [email protected]
    5 thành một đối tượng với các giá trị của mỗi màu

  • Chuyển đổi một chuỗi màu

    Geeks for Geeks!!!
    To contribute articles contact us at  
    [email protected]
    6 thành một đối tượng với các giá trị của mỗi màu

Từ đầu ra ở trên, chúng ta có thể thấy ngoại lệ trong địa chỉ email có ký hiệu đặc biệt “@” không được mã hóa & hiển thị giống như trong đầu vào & phần còn lại của văn bản đã được mã hóa

Bây giờ, nếu chúng ta muốn chuyển đổi văn bản được mã hóa, tức là. , bản mã thành văn bản có thể đọc bình thường thì chúng ta phải sử dụng hàm unescape() sẽ giải mã văn bản được mã hóa. Giải mã là quá trình chuyển đổi bản mã thành văn bản thuần túy.  

2. hàm unescape(). Hàm này lấy một chuỗi làm tham số duy nhất và sử dụng nó để giải mã chuỗi đó được mã hóa bởi hàm escape(). Chuỗi thập lục phân trong chuỗi được thay thế bằng các ký tự mà chúng đại diện khi được giải mã thông qua hàm unescape()

cú pháp

unescape(string)

Thông số. Chức năng này chấp nhận một tham số duy nhất

  • sợi dây. Tham số này chứa chuỗi sẽ được giải mã

Giá trị trả về. Trả về một chuỗi đã giải mã

Ghi chú. Chức năng này chỉ giải mã các ký tự đặc biệt, chức năng này không được dùng nữa

ngoại lệ. @ – +. / * _

ví dụ 1. Trong ví dụ này, chúng tôi đã sử dụng ký tự đặc biệt để xem các thay đổi.  

Javascript




Geeks for Geeks!!!
To contribute articles contact us at  
[email protected]
7

Geeks for Geeks!!!
To contribute articles contact us at  
[email protected]
8
Geeks for Geeks!!!
To contribute articles contact us at  
[email protected]
9

Geeks for Geeks!!!
To contribute articles contact us at  
[email protected]
8
Geeks for Geeks!!!
To contribute articles contact us at  
[email protected]
5
Geeks for Geeks!!!
To contribute articles contact us at  
[email protected]
6
Geeks%20for%20Geeks%21%21%21
To%20contribute%20articles%20contact%20us%20atcontribute
@geeksforgeeks.org 
1

Geeks for Geeks!!!
To contribute articles contact us at  
[email protected]
8

Geeks for Geeks!!!
To contribute articles contact us at  
[email protected]
8
Geeks%20for%20Geeks%21%21%21
To%20contribute%20articles%20contact%20us%20atcontribute
@geeksforgeeks.org 
4____45
Geeks%20for%20Geeks%21%21%21
To%20contribute%20articles%20contact%20us%20atcontribute
@geeksforgeeks.org 
6

Geeks for Geeks!!!
To contribute articles contact us at  
[email protected]
8

Geeks for Geeks!!!
To contribute articles contact us at  
[email protected]
8
Geeks%20for%20Geeks%21%21%21
To%20contribute%20articles%20contact%20us%20atcontribute
@geeksforgeeks.org 
9

Geeks for Geeks!!!
To contribute articles contact us at  
[email protected]
8
unescape(string)
1

Geeks for Geeks!!!
To contribute articles contact us at  
[email protected]
8
Geeks for Geeks!!!
To contribute articles contact us at  
[email protected]
5
Geeks for Geeks!!!
To contribute articles contact us at  
[email protected]
70
unescape(string)
5

________ 372 ________ 373 ________ 41

unescape(string)
9

Geeks for Geeks!!!
To contribute articles contact us at  
[email protected]
72

đầu ra

Geeks for Geeks!!!
To contribute articles contact us at  
[email protected]

Từ ví dụ trên, chúng ta có thể thấy bản mã được giải mã thành văn bản thuần túy bằng cách sử dụng hàm unescape()

ví dụ 2.  

Javascript




Geeks for Geeks!!!
To contribute articles contact us at  
[email protected]
7

Geeks for Geeks!!!
To contribute articles contact us at  
[email protected]
8
Geeks for Geeks!!!
To contribute articles contact us at  
[email protected]
9

Geeks for Geeks!!!
To contribute articles contact us at  
[email protected]
8
Geeks for Geeks!!!
To contribute articles contact us at  
[email protected]
81
Geeks for Geeks!!!
To contribute articles contact us at  
[email protected]
82
Geeks%20for%20Geeks%21%21%21
To%20contribute%20articles%20contact%20us%20atcontribute
@geeksforgeeks.org 
0
Geeks%20for%20Geeks%21%21%21
To%20contribute%20articles%20contact%20us%20atcontribute
@geeksforgeeks.org 
6

Geeks for Geeks!!!
To contribute articles contact us at  
[email protected]
8
Geeks%20for%20Geeks%21%21%21
To%20contribute%20articles%20contact%20us%20atcontribute
@geeksforgeeks.org 
4____387
Geeks for Geeks!!!
To contribute articles contact us at  
[email protected]
88

Geeks for Geeks!!!
To contribute articles contact us at  
[email protected]
8

Geeks for Geeks!!!
To contribute articles contact us at  
[email protected]
8
Geeks for Geeks!!!
To contribute articles contact us at  
[email protected]
91

Geeks for Geeks!!!
To contribute articles contact us at  
[email protected]
8
Geeks%20for%20Geeks%21%21%21
To%20contribute%20articles%20contact%20us%20atcontribute
@geeksforgeeks.org 
4____45
Geeks%20for%20Geeks%21%21%21
To%20contribute%20articles%20contact%20us%20atcontribute
@geeksforgeeks.org 
6

Geeks for Geeks!!!
To contribute articles contact us at  
[email protected]
8

Geeks for Geeks!!!
To contribute articles contact us at  
[email protected]
8
Geeks for Geeks!!!
To contribute articles contact us at  
[email protected]
98

Geeks for Geeks!!!
To contribute articles contact us at  
[email protected]
8
Geeks%20for%20Geeks%21%21%21
To%20contribute%20articles%20contact%20us%20atcontribute
@geeksforgeeks.org 
4____381
Geeks for Geeks!!!
To contribute articles contact us at  
[email protected]
82

Geeks for Geeks!!!
To contribute articles contact us at  
[email protected]
8

Geeks for Geeks!!!
To contribute articles contact us at  
[email protected]
8
Geeks for Geeks!!!
To contribute articles contact us at  
[email protected]
91

Geeks for Geeks!!!
To contribute articles contact us at  
[email protected]
8
Geeks%20for%20Geeks%21%21%21
To%20contribute%20articles%20contact%20us%20atcontribute
@geeksforgeeks.org 
4____388
Geeks%20for%20Geeks%21%21%21
To%20contribute%20articles%20contact%20us%20atcontribute
@geeksforgeeks.org 
6

Geeks for Geeks!!!
To contribute articles contact us at  
[email protected]
8

Geeks for Geeks!!!
To contribute articles contact us at  
[email protected]
8
Geeks%20for%20Geeks%21%21%21
To%20contribute%20articles%20contact%20us%20atcontribute
@geeksforgeeks.org 
72

Geeks for Geeks!!!
To contribute articles contact us at  
[email protected]
8
Geeks%20for%20Geeks%21%21%21
To%20contribute%20articles%20contact%20us%20atcontribute
@geeksforgeeks.org 
74

Geeks for Geeks!!!
To contribute articles contact us at  
[email protected]
8
Geeks for Geeks!!!
To contribute articles contact us at  
[email protected]
82
Geeks%20for%20Geeks%21%21%21
To%20contribute%20articles%20contact%20us%20atcontribute
@geeksforgeeks.org 
77
unescape(string)
5

Geeks for Geeks!!!
To contribute articles contact us at  
[email protected]
72
Geeks%20for%20Geeks%21%21%21
To%20contribute%20articles%20contact%20us%20atcontribute
@geeksforgeeks.org 
00
Geeks%20for%20Geeks%21%21%21
To%20contribute%20articles%20contact%20us%20atcontribute
@geeksforgeeks.org 
01

Geeks for Geeks!!!
To contribute articles contact us at  
[email protected]
8
Geeks%20for%20Geeks%21%21%21
To%20contribute%20articles%20contact%20us%20atcontribute
@geeksforgeeks.org 
4____387
Geeks for Geeks!!!
To contribute articles contact us at  
[email protected]
88

Geeks for Geeks!!!
To contribute articles contact us at  
[email protected]
8

Geeks for Geeks!!!
To contribute articles contact us at  
[email protected]
8
Geeks for Geeks!!!
To contribute articles contact us at  
[email protected]
91

Geeks for Geeks!!!
To contribute articles contact us at  
[email protected]
8
Geeks%20for%20Geeks%21%21%21
To%20contribute%20articles%20contact%20us%20atcontribute
@geeksforgeeks.org 
4____45
Geeks%20for%20Geeks%21%21%21
To%20contribute%20articles%20contact%20us%20atcontribute
@geeksforgeeks.org 
6

Geeks for Geeks!!!
To contribute articles contact us at  
[email protected]
8

Geeks for Geeks!!!
To contribute articles contact us at  
[email protected]
8
Geeks for Geeks!!!
To contribute articles contact us at  
[email protected]
98

Geeks for Geeks!!!
To contribute articles contact us at  
[email protected]
8
Geeks%20for%20Geeks%21%21%21
To%20contribute%20articles%20contact%20us%20atcontribute
@geeksforgeeks.org 
4____381
Geeks for Geeks!!!
To contribute articles contact us at  
[email protected]
82

unescape(string)
9

đầu ra

Geeks for Geeks!!!
To contribute articles contact us at  
[email protected]
3

Sự khác biệt giữa hàm thoát () và thoát ()

 

không thoát()

thoát khỏi()

1

Hàm unescape() được sử dụng để giải mã chuỗi được mã hóa bởi hàm escape()

Hàm escape() trong JavaScript được sử dụng để mã hóa một chuỗi. Sử dụng hỗ trợ ký tự ASCII, nó tạo ra một chuỗi di động để nó có thể được truyền qua bất kỳ mạng nào tới bất kỳ máy tính nào

HTML thoát unescape là gì?

Html. html. unescape() thay thế tên thực thể hoặc số thực thể của các ký tự HTML dành riêng bằng biểu diễn ký tự ban đầu . Ví dụ, chuỗi

Hàm unescape() và escape() là gì?

Hàm escape() được sử dụng để mã hóa một chuỗi, đảm bảo an toàn khi sử dụng trong URL. Hàm unescape() dùng để giải mã một chuỗi mã hóa .

Có ký tự thoát trong HTML không?

Trong HTML, XHTML hoặc XML, bạn có thể sử dụng ký tự thoát để thể hiện bất kỳ ký tự Unicode nào chỉ bằng các chữ cái ASCII . Thoát ký tự được sử dụng trong đánh dấu bao gồm tham chiếu ký tự số (NCR) và tham chiếu ký tự được đặt tên. Tham chiếu ký tự số ở định dạng thập lục phân.

Những ký tự nào phải được thoát trong HTML?

Nếu các thuộc tính HTML không được trích dẫn, thì một số ký tự nhất định, quan trọng nhất là khoảng trắng, chẳng hạn như dấu cách và tab , phải được thoát bằng các thực thể.