-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathhtmlSpecialChars.js
21 lines (16 loc) · 1.2 KB
/
htmlSpecialChars.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
You are a(n) novice/average/experienced/professional/world-famous Web Developer (choose one) who owns a(n) simple/clean/slick/beautiful/complicated/professional/business website (choose one or more) which contains form fields so visitors can send emails or leave a comment on your website with ease. However, with ease comes danger. Every now and then, a hacker visits your website and attempts to compromise it through the use of XSS (Cross Site Scripting). This is done by injecting script tags into the website through form fields which may contain malicious code (e.g. a redirection to a malicious website that steals personal information).
Mission
Your mission is to implement a function htmlspecialchars() that converts the following potentially harmful characters:
< --> <
> --> >
" --> "
& --> &
Good luck :D
Extension
If you are an experienced Javascript programmer, try shortening your code as much as possible and optimise it to minimise run time. Experienced programmers should be able to complete this exercise in one line of code.
*/
//Answer//
function htmlspecialchars(form) {
return form.replace(/[&]/g,'&').replace(/[<]/g,'<').replace(/[>]/g,'>').replace(/["]/g,'"')
}