1

I would like to convert this text éàè to éàè using javascript.

I tried $('<textarea />').text("éàe").html(); but it doesn't encode the accented characters...

The org.apache.commons.lang3.StringEscapeUtils.escapeHtml works correctly in Java, is there an equivalent for javascript ?

Olivier Boissé
  • 15,834
  • 6
  • 38
  • 56
  • 1
    Do you know *HOW* Apache does it in Java? It builds a giant cheat sheet: http://grepcode.com/file/repo1.maven.org/maven2/commons-lang/commons-lang/2.1/org/apache/commons/lang/Entities.java#Entities.0HTML40_ARRAY – Nate Apr 25 '17 at 15:36
  • 1
    yeah I just saw the library [he.js](https://github.com/mathiasbynens/he) does the same thing, I wanted to not use an external library – Olivier Boissé Apr 25 '17 at 15:51
  • Out of curiosity, what's the ultimate goal? Entities for common Latin characters were barely necessary in the late 1990s and 20 decades have passed. – Álvaro González Apr 25 '17 at 15:58
  • Also, do you strictly need named entities? Are numeric entities an option? – Álvaro González Apr 25 '17 at 15:59
  • we have some problem of characters encoding when submitting a form field to the server, for some users the special characters `éàè...` are not correctly decoded by the server, I can't reproduce the problem on my desk with ff and chrome (it works correctly), I suspect a problem of client side encoding. The quick fix is to send html entities to the server – Olivier Boissé Apr 25 '17 at 16:11
  • 1
    It feels like you aren't using UTF-8 explicitly but relying on defaults... – Álvaro González Apr 25 '17 at 16:25
  • I thought the same thing, but threre is a `<%@ page contentType="text/html; charset=UTF-8" %>` at the begin of the jsp – Olivier Boissé Apr 25 '17 at 17:16
  • That's a manifesto to declare you are using UTF-8. You still need to actually use it ;-) – Álvaro González Apr 26 '17 at 11:11
  • how can I specify to use it ? – Olivier Boissé Apr 26 '17 at 11:26
  • Well... I can refer you to [UTF-8 all the way through](http://stackoverflow.com/questions/279170/utf-8-all-the-way-through#279279). It's a PHP question but it contains valuable information anyway. – Álvaro González Apr 26 '17 at 14:14

1 Answers1

0

If you use ES6, you might be interested in tagged template literals...

function htmlentities(raw) {
  const str = raw[0];

  return str.replace(/é/g, '&eacute;')
            .replace(/à/g, '&agrave;')
            .replace(/è/g, '&egrave;');
}

console.log(htmlentities`éàè`);
Badacadabra
  • 8,043
  • 7
  • 28
  • 49
  • I want to convert all the accented charaters, I realized there is no native function to do this, I will use the [he.js](https://github.com/mathiasbynens/he) library – Olivier Boissé Apr 25 '17 at 17:20