edit: fixed this myself, it was a silly mistake.
So, I have a problem with my code. It works, and after trying with case this works better, but it only works for one character at a time, which is confusing for me because I thought if statements execute regardless of the previous if statement being true or false (unlike else and else if), and I’m returning the new string everytime EDIT: THIS was my problem. I was returning every time so I was exiting the if statement. Fixed.
…so I should be able to use it updated in the next if statement.
Could someone point me in the right direction as to how to solve this
Thanks so much in advance
Here’s my code:
function convertHTML(str) {
var newStr = str;
console.log(str);
var regex = /\W/gi;
if (str.match(regex) == null){
console.log(str);
// return str;
}
else if (str.match(regex) != -1){
var match = str.match(regex);
if (match.includes("&")){
newStr = newStr.replace(/&/gi, "&");
console.log(newStr);
// return newStr;
}
if (match.includes('"')){
newStr = newStr.replace(/"/gi, """);
console.log(newStr);
// return newStr;
}
if (match.includes("'")){
newStr = newStr.replace(/'/gi, "'");
console.log(newStr);
//return newStr;
}
if (match.includes("<")){
newStr = newStr.replace(/</gi, "<");
console.log(newStr);
//return newStr;
}
if (match.includes(">")){
newStr = newStr.replace(/>/gi, ">");
console.log(newStr);
//return newStr;
}
}
return newStr;
}
convertHTML("Dolce & Gabbana");
convertHTML("<>");
//convertHTML("abc");
//convertHTML("Schindler's list");