Search code examples
javascriptunicodehtml-entitiestemplate-literals

Using HTML entities like – or — with template literals


Having this template literal:

`HH:MM${myValue? '' : ', XXX'}${anotherValue} - ` 

I want to replace the hyphen at the end (-) with en dash () by using its code –.

I tried several methods but none of them worked. For example:

`HH:MM${myValue? '' : ', XXX'}${anotherValue} – ` 

and

`HH:MM${myValue? '' : ', XXX'}${anotherValue}  + '–'`

Any ideas how to do this?


Solution

  • You can't use html entities in a template literal. But you can use the unicode equivalent of the –:

    `HH:MM${myValue? '' : ', XXX'}${anotherValue} \u2013 `
    

    See here: https://www.fileformat.info/info/unicode/char/2013/index.htm