<p>I have a <a href="https://github.com/carlcs/craft-helpers" rel="nofollow noreferrer">Craft Helpers</a> plugin on GitHub, which has a <code>htmlEntityDecode</code> Twig filter.</p>

<pre><code>{{ value|htmlEntityDecode}}
</code></pre>

CC BY-SA 3.0
2017-01-27T12:44:22.063Z
Thanks,

htmlEntityDecode did the trick.

Didn't seem to work with the striptags filter by the way.
S. Connolly22
CC BY-SA 3.0
2017-01-27T12:44:55.650Z
I up-voted but it wont display as I'm a noob.
S. Connolly22
CC BY-SA 3.0
2017-01-27T17:42:27.120Z
Sorry, should have made clear that `striptags` would further get rid of HTML tags after you converted all your HTML entities. Seems like you have non, as I could have already guessed from your question. Will get rid of it in the answer. Oh and thanks for the up-vote anyway :)
carlcs
CC BY-SA 3.0
2017-01-27T12:16:45.870Z
2017-01-27T17:42:40.153Z
2017-01-27T17:42:40.153Z
carlcs
<p>Apologies if this has been asked before, I can only find questions dealing with the reverse scenario (i.e. replacing special characters with html entities)</p>

<p>I need to output special characters as part of a JavaScript array using Craft. Currently the values are output with all special characters replaced by their html equivalent. (e.g.: ampersands are replaced by &amp;amp )</p>

<p>I have tried restoring the special characters using craft replace but without success.</p>

<p>The following code works to replace text, but does not, for example, replace &amp;amp with &amp;, or rather perhaps it does not distinguish these values as distinct.</p>

<pre><code>&lt;script&gt;

    var myVariable = [{% for value in myArray %}"{{ value|replace('raw-html-entity', 'special-character') }}"{%- if not loop.last -%},{% endif %}{% endfor %}];

&lt;/script&gt;
</code></pre>

<p>This code is being used for an array which is used for jQuery autocomplete values, so raw html is not appropriate.</p>

<p>Any suggestions appreciated.</p>

<templating><javascript><array><replace>
How to replace a html entity within script tags using Craft replace
# How to replace a html entity within script tags using Craft replace

(Asked by: username_0 on 2017-01-27)

Apologies if this has been asked before, I can only find questions dealing with the reverse scenario (i.e. replacing special characters with html entities)
I need to output special characters as part of a JavaScript array using Craft. Currently the values are output with all special characters replaced by their html equivalent. (e.g.: ampersands are replaced by & )
I have tried restoring the special characters using craft replace but without success.
The following code works to replace text, but does not, for example, replace & with &, or rather perhaps it does not distinguish these values as distinct.


This code is being used for an array which is used for jQuery autocomplete values, so raw html is not appropriate.
Any suggestions appreciated.

**Tags:** templating, javascript, array, replace

---

## 1 answers

### Answer by username_1 on 2017-01-27. Score: 2 | Accepted answer

I have a Craft Helpers plugin on GitHub, which has a htmlEntityDecode Twig filter.
{{ value|htmlEntityDecode}}

> **Comments:**
> **username_0**: Thanks,  htmlEntityDecode did the trick.  Didn't seem to work with the striptags filter by the way.  
> **username_0**: I up-voted but it wont display as I'm a noob.  
> **username_1**: Sorry, should have made clear that `striptags` would further get rid of HTML tags after you converted all your HTML entities. Seems like you have non, as I could have already guessed from your question. Will get rid of it in the answer. Oh and thanks for the up-vote anyway :)  

---

