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 1290 error when importing file data in mysql

Error scenario Use the mysql command in cmd to ad...

4 principles for clean and beautiful web design

This article will discuss these 4 principles as t...

What do CN2, GIA, CIA, BGP and IPLC mean?

What is CN2 line? CN2 stands for China Telecom Ne...

CentOS 8 custom directory installation nginx (tutorial details)

1. Install tools and libraries # PCRE is a Perl l...

Several mistakes that JavaScript beginners often make

Table of contents Preface Confusing undefined and...

A Deep Understanding of Angle Brackets in Bash (For Beginners)

Preface Bash has many important built-in commands...

How to implement Ajax concurrent request control based on JS

Table of contents Preface Ajax serial and paralle...

How to use CSS to write different styles according to sub-elements

The effect we need to achieve: What is needed The...

Detailed explanation of HTML page header code example

Knowledge point 1: Set the base URL of the web pa...

Detailed steps for running springboot project in Linux Docker

Introduction: The configuration of Docker running...

Solution to the lack of my.ini file in MySQL 5.7

What is my.ini? my.ini is the configuration file ...

Tutorial on downloading, installing, configuring and using MySQL under Windows

Overview of MySQL MySQL is a relational database ...

Simple example of HTML checkbox and radio style beautification

Simple example of HTML checkbox and radio style b...

Vue implements seamless scrolling of lists

This article example shares the specific code of ...