I am looping through an array of letters and am trying to find only &, <, >, " and ' characters to replace them with their HTML entities...
Here is my code
function convertHTML(str) {
// html entity switch block
function entify(char){
alert(char);
switch (char) {
case '&':
return '&';
case '<':
return '<';
case '>':
return '>';
case '"':
return '"';
case '\'':
return ''';
}
}
// convert string into an array
str = str.split('');
// loop through the array
for (var i = 0; i < str.length; i++) {
// compare each to the regular expression, if it matches then it needs to be replaced with an html entity
console.log(str[i]);
str[i] = str[i].replace(/[&<>"']/g, entify);
}
str = str.join('');
console.log(str);
return str;
}
convertHTML("&");
Unfortunately my regular expression doesn't seem to be picking up the characters and I'm not even triggering the entify
function.
Why is that?