views:

117

answers:

2

I have strings like

var str = 'One & two & three';

rendered into HTML by the web server. I need to transform those strings into

'One & two & three'

Currently, that's what I am doing (with help of jQuery):

$(document.createElement('div')).html('{{ driver.person.name }}').text()

However I have an unsettling feeling that I am doing it wrong. I have tried

unescape("&")

but it doesn't seem to work, neither do decodeURI/decodeURIComponent.

Are there any other, more native and elegant ways of doing so?

+2  A: 

Do you need to decode all encoded HTML entities or just & itself?

If you only need to handle & then you can do this:

var decoded = encoded.replace(/&/g, '&');

If you need to decode all HTML entities then what you've got is fine, although you can do it without jQuery if you want to:

var div = document.createElement('div');
div.innerHTML = encoded;
var decoded = div.firstChild.nodeValue;
LukeH
Preferably, all of them. Thanks.
Art
A: 

The huge function included in this article seems to work fine: http://blogs.msdn.com/b/aoakley/archive/2003/11/12/49645.aspx
I don't think that's the most clever solution but works.

Matias