Convert the characters &, <,>, (double quotes), and ' (single quotes) in the string to their corresponding HTML entities.
If you are stuck, remember to use the ultimate Read-Search-Ask. Try to communicate programming ideas with others, but write your own code.
For example:convert(Dolce & Gabbana) should return Dolce & Gabbana.
convert(Hamburgers < Pizza < Tacos) should return Hamburgers < Pizza < Tacos.
convert(Sixty > twelve) should return Sixty > twelve.
convert('Stuff in quotation marks') should return Stuff in "ation marks"ation.
convert(Shindler's List) should return Shindler's List.
convert(<>) should return <>.
convert(abc) should return abc.
Answer:
| method | describe |
|---|---|
| RegExp | It is the abbreviation of regular expressions. |
| replace() | Replace substrings that match the regular expression. |
| HTML character entity | Reserved characters in HTML must be replaced with character entities. |
function convert(str) { var list={ &:&, <:<, >:>, '':", ':', }; for(var key in list){ str=str.replace(new RegExp(key,g),list[key]); } return str;}convert(Dolce & Gabbana);Running results:
Dolce & Gabbana
Online testing:
html symbol to entity algorithm challenge | w3cschool
SummarizeThis is the end of this article about the challenge of html symbols to entity algorithms. For more relevant html symbols to entity content, please search for previous articles from Wulin.com or continue to browse the related articles below. I hope everyone will support Wulin.com in the future!