Javascript - Thay ký tự HTML

Viết một hàm JavaScript để thay ký tự HTML

Mã nguồn:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JavaScript program to convert the letters of a given string  in alphabetical order.</title>
</head>
<script>
	function escape_HTML(html_str) {
    'use strict';

    return html_str.replace(/[&<>"]/g, function (tag) {
		var chars_to_replace = {
            '&': '&',
            '<': '<',
            '>': '>',
            '"': '"'
        };

		return chars_to_replace[tag] || tag;
	});
}

document.write(escape_HTML('&lt;a href=&quot;javascript-string-exercise-17.php&quot; target=&quot;_blank&quot;&gt;'));

</script>
<body>

</body>
</html> 

Xem ví dụ

Lưu đồ thuật toán:

Flowchart: JavaScript- Escape a HTML string


Tư vấn lộ trình CNTT 🤖