开发者

how to decode a string in a variable in javascript?

开发者 https://www.devze.com 2022-12-28 18:20 出处:网络
I have code in javascript: var location = \'"HCM - NYC (New York, NY)"\'; td_Details.innerText = location;

I have code in javascript:

var location = '"HCM - NYC (New York, NY)"';
td_Details.innerText = location;

Now I want to decode the text location to

"H开发者_高级运维CM - NYC (New York, NY)"

Please advice. Thanks.


There is no specific function in JavaScript which will decode HTML entities, however you can assign an innerHTML property to an element and then read it back.

x = document.createElement('div');
x.innerHTML = ""test"";
console.log(x.innerHTML); // => "test"

This will work for any HTML entities, not just "

edit:

As pointed out below, you're half-way there, you're just using the wrong property.

Change:

td_Details.innerText = location;

to:

td_Details.innerHTML = location;

For future reference, innerHTML is available in all browsers. innerText is not.


To remove the " just use the following:

location = location.replace(/"/g, '');

You may have actually meant to include the quotes in your output. To do so, do this instead:

location = location.replace(/"/g, '"');
0

精彩评论

暂无评论...
验证码 换一张
取 消