Convert HTML Entities - need help with regxp

Convert HTML Entities - need help with regxp
0.0 0

#1

I am trying to solve the problem using regexp. I have partly solved the challenge apart from my regex is not picking up the single and double quotes. I am confused. Here is my code:

var charMap = {
  ' & ': " & ",
  ' < ': " &lt; ",
  ' > ': " &gt; ",
  " ' ": " &apos; ",
  ' " ': " &quot; "
};

function convertHTML(str) {
  // &colon;&rpar;
  var newString = str.replace(/\W+/g, function myFunc(x){
    return charMap[x];
  });
  return newString;
}

Any advice would be very appreciated.


#2

My regexp was wrong. I did not take out the white space and for some reason that only people who understand the arcane logic of regex it was not detecting the single and double quotes.

I managed to figure it out myself (with the help of stackoverflow of course).

The correct code is:

WARNING SPOILER!!!
’’’

/[^a-zA-Z0-9\s]/g

‘’’