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

Solution to forgetting the MYSQL database password under MAC

Quick solution for forgetting MYSQL database pass...

Code comment writing standards during web page production

<br />I have summarized the annotation writi...

Sample code for CSS dynamic loading bar effect

Using the knowledge of CSS variables, I will dire...

Lombok implementation JSR-269

Preface Introduction Lombok is a handy tool, just...

Detailed deployment of docker+gitlab+gitlab-runner

environment Server: centos7 Client: window Deploy...

A brief discussion on several ways to pass parameters in react routing

The first parameter passing method is dynamic rou...

React Router 5.1.0 uses useHistory to implement page jump navigation

Table of contents 1. Use the withRouter component...

How to detect file system integrity based on AIDE in Linux

1. AIDE AIDE (Advanced Intrusion Detection Enviro...

Dealing with the problem of notes details turning gray on web pages

1. In IE, if relative positioning is used, that is...

Detailed tutorial on installing CUDA9.0 on Ubuntu16.04

Preface: This article is based on the experience ...

Nginx stream configuration proxy (Nginx TCP/UDP load balancing)

Prelude We all know that nginx is an excellent re...

HTML 5 Preview

<br />Original: http://www.alistapart.com/ar...

js implements array flattening

Table of contents How to flatten an array 1. Usin...

How to Develop a Progressive Web App (PWA)

Table of contents Overview Require URL of the app...