admin管理员组

文章数量:1310074

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?

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?

Share Improve this question edited Jul 3, 2022 at 16:27 Daniel Widdis 9,14013 gold badges48 silver badges68 bronze badges asked Jun 25, 2018 at 13:15 Leo MessiLeo Messi 6,19622 gold badges79 silver badges154 bronze badges 5
  • have you tried HH:MM${myValue? '' : ', XXX'}${anotherValue} –? – Tanmay Commented Jun 25, 2018 at 13:20
  • Some clarifying questions: Why can't you just replace the - with a ? Why are you using –? What do you mean is doesn't "work"? – James Kraus Commented Jun 25, 2018 at 13:21
  • I know that it works like that but I need to do it without using that method – Leo Messi Commented Jun 25, 2018 at 13:28
  • 2 You'll need to add more information about what you're trying to do and why that doesn't work. – James Kraus Commented Jun 25, 2018 at 13:37
  • @JamesKraus There could be many different reasons why someone needs to use an html entity – Ringo Commented Oct 13, 2020 at 4:22
Add a ment  | 

1 Answer 1

Reset to default 10

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

本文标签: javascriptUsing HTML entities like ampndash or ampmdash with template literalsStack Overflow