0

-------------------------------------------------------------UPDATE----------------------------------------------------------

What I want - Eleborated:

How to fetch OR import all the special characters and their entity names from the below mentioned URL's to my htmlEscape function, I don't want to copy paste every single character and its entity name into .replace() function manually, repeating the .replace() 300 times is difficult and NOT very programmatic.

OR

Is there a way to generate entity name from its character for instance I've a character < and somehow a function generates its entity code &lt.

HTML5 Special Characters: https://dev.w3.org/html5/html-author/charref

HTML3.2 Special Character: https://www.utexas.edu/learn/html/spchar.html

Note: The task I've require NO third party library.

Thanks, Help will be appreciate.

Backgrond:

Hey, I'm writing a custom function which will take the symbol and replace it with its entity name or number code, also I'm NOT looking for any 3rd party library. The symbol specification are from HTML 3.2 to HTML5, check the links bwlow. when i googled HTML special characters it turn out a lot more than i expected, if i keep repeating the code as below then i have to write every single symbol and its entity name or number code.

What I want - OLD:

Is there a better way to get the symbols and replace them with their entity name or number code because repeating the .replace 300 times is difficult and NOT very programmatic.

function htmlEscape(symbol)
{
    return symbol
        .replace(/-/g, "&ndash;").replace(/—/g, "&mdash;")
        .replace(/¡/g, "&iexcl;").replace(/¿/g, "&iquest;")
        .replace(/“/g, "&ldquo;").replace(/”/g, "&rdquo;")
        .replace(/‘/g, "&isquo;").replace(/’/g, "&rsquo;")
        .replace(/«/g, "&laquo;").replace(/»/g, "&raquo;")
        .replace(/"/g, "&quot;").replace(/'/g, "&#039;")
}
Wcan
  • 840
  • 1
  • 10
  • 31
  • If it's more trouble than it's worth, don't be afraid to use a library. [Here's one](https://www.npmjs.com/package/html-entities) – 4castle Jul 02 '16 at 01:15
  • Dang this isn't an exact duplicate since he wants to replace more than the "troublesome" HTML characters... I had a good answer written up when the question got closed. – Jeremy J Starcher Jul 02 '16 at 01:27
  • codes intelligently ordered https://rack.pub/codes – Ronnie Royston Jul 02 '16 at 04:52
  • Hey to the guy who closed my question, I've checked the answer for which u have closed my question as duplicate. The best answer with 48 upvotes is replacing &, <, > sign to their respective entity names, please note in order to replace < character it has to there inside the fuction to match it with incoming character, my qestion was "Is there a better way to get the symbols and replace them with their entity name or number code because repeating the .replace() 300 times is difficult" I didn't see where he provide a method to import all HTML character & entity name inside my function. – Wcan Jul 03 '16 at 00:40
  • @Ron Royston, thanks I already have the codes and their entity names but what I'm asking is how do i import them inside my function instead of copying and pasting every single character and their entity name or number code manually. – Wcan Jul 03 '16 at 00:44
  • Also instead of replacing the charatcers to their respective entity name if i want to strip or trim then i must have that character inside my function to match it with incoming character isn't it ?? If NOT how do i trim them without knowing what needs to be trimmed ? – Wcan Jul 03 '16 at 00:55

0 Answers0