HTML symbol to entity algorithm challenge

HTML symbol to entity algorithm challenge

challenge:

Converts the characters &, <, >, " (double quote), and ' (single quote) in a string to their corresponding HTML entities.

If you get stuck, use Read-Search-Ask. Try exchanging programming ideas with others, but write your own code.

For example:

convert("Dolce & Gabbana") should return Dolce &​amp; Gabbana.
convert("Hamburgers < Pizza < Tacos") should return Hamburgers <​lt; Pizza <​lt; Tacos.
convert("Sixty > twelve") should return Sixty >​gt; twelve.
convert('Stuff in "quotation marks"') should return Stuff in "quotation marks" .
convert("Shindler's List") should return Shindler's List.
convert("<>") should return &​lt;&​gt;.
convert("abc") should return abc.

Answer:

method describe
RegExp It is an abbreviation of regular expression.
replace() Replaces substrings that match a regular expression.
HTML Character Entities Reserved characters in HTML must be replaced with character entities.

function convert(str) {
 var list = {
    "&":"&amp;",
    "<":"&lt;",
    ">":"&gt;",
    '"':"&quot;,
    "'":"&apos;",   
  };
  for(var key in list){
    str=str.replace(new RegExp(key,"g"),list[key]);
  }
  return str;
}

convert("Dolce & Gabbana");

Running results:

Dolce & Gabbana

Online test:

HTML symbol to entity algorithm challenge | w3cschool

Summarize

This is the end of this article about the challenge of converting HTML symbols to entities. For more relevant content about converting HTML symbols to entities, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future!

<<:  How to create a MySQL master-slave database using Docker on MacOS

>>:  Detailed example of locating and optimizing slow query sql in MySQL

Recommend

html option disable select select disable option example

Copy code The code is as follows: <select> ...

Detailed explanation of the use of base tag in HTML

In requireJS, there is a property called baseURL....

A brief summary of vue keep-alive

1. Function Mainly used to preserve component sta...

Problems installing TensorRT in docker container

Uninstall the installed version on Ubuntu: sudo a...

How to use Font Awesome 5 in Vue development projects

Table of contents Install Dependencies Configurat...

MySQL replication table details and example code

MySQL replication table detailed explanation If w...

Sample code for making a drop-down menu using pure CSS

Introduction: When I looked at interview question...

Detailed tutorial on installing CentOS, JDK and Hadoop on VirtualBox

Table of contents 1. Prerequisites 1.1 Supported ...

Introduction to document.activeELement focus element in JavaScript

Table of contents 1. The default focus is on the ...

Detailed explanation based on event bubbling, event capture and event delegation

Event bubbling, event capturing, and event delega...

Detailed process of SpringBoot integrating Docker

Table of contents 1. Demo Project 1.1 Interface P...

Detailed explanation of the basic commands of Docker run process and image

Table of contents 1. Run workflow 2. Basic comman...

VMware15 installation of Deepin detailed tutorial (picture and text)

Preface When using the Deepin user interface, it ...

Implementation code of short video (douyin) watermark removal tool

Table of contents 1. Get the first link first 2. ...