fcc-Learning Note Convert HTML entities
1> recently studied and practiced the FCC topics. This is really a good comparison, recommended to everyone.
2> address of the Chinese version: https://www.freecodecamp.cn/, English version of the address: https://www.freecodecamp.org
3> this time to write about a JS problem, called convert HTML entities.
The rules require the following:
Converts characters in a string,,, &
<
>
"
(double quotation marks), and ‘
(single quotation marks) to their corresponding HTML entities.
4> The code I wrote was implemented as follows:
function convert (str) { //:) //Regular expression array var arr=[/&/i,/</i,/>/i,/"/i,/'/I]; The corresponding replacement HTML element var duiarr=["&", "<", ">", "'", "'"; for (Var i=0;i<arr.length;i++) for (var j=0;j<str.length;j++) { if (arr[i].test (Str[j])) { str =str.replace (Str[j],duiarr[i]); } } return str;} Convert ("Dolce & Gabbana"); Convert ("Hamburgers < Pizza < Tacos"); Convert ("Sixty > Twelve"); Convert (' Stuff In "quotation marks"); Convert ("Shindler's List"); Convert ("<>"); CONVERT ("abc");
5> write bad also need to improve, look forward to everyone's point, common progress!
fcc-Learning Note Convert HTML entities