简体   繁体   English

如何在 JavaScript 中使用 unescape() function?

[英]How to use unescape() function inside JavaScript?

I have a JSP page in which I have JavaScript function that will be called when a link is clicked.我有一个 JSP 页面,其中我有 JavaScript function 将在单击链接时调用。 Now, when the value reaches the JavaScript function, the apostrophe is encoded.现在,当值达到 JavaScript function 时,撇号被编码。

Example:例子:

Name#039;s

Before # there is &, which originally should be: #之前有&,本来应该是:

Name's

I have used the unescape() decode function, but nothing seems to work.我使用了unescape()解码 function,但似乎没有任何效果。 In the end, I had to delete the characters and add the apostrophe.最后,我不得不删除字符并添加撇号。 Does anyone know a fix for this?有谁知道解决这个问题? Is it that JSP doesn't support encoding for & ?是不是 JSP 不支持&的编码? When I was writing the same encode value in this page, it changed the symbol to the apostrophe, which is what I wanted in my code.当我在此页面中编写相同的编码值时,它会将符号更改为撇号,这正是我在代码中想要的。

Built-in Javascript function such as unescape() , decodeURIComponent() has nothing to do with the string you are working on, because the one you are looking to decode are HTML entites.内置 Javascript function 如unescape()decodeURIComponent()与您正在处理的字符串无关,因为您要解码的是 Z4C4AD5FCA2E7A1AA4Zents.

There are no HTML entites decoder available in Javascript, but since you are working with a browser, if the string is considered safe , you may do the following (in JQuery, for example) Javascript 中没有可用的 HTML 实体解码器,但由于您使用的是浏览器,如果字符串被认为是安全的,您可以执行以下操作(例如在 JQuery 中)

var str = $('<p />').html(str).text();

It bascially insert the string as HTML to a <p> element and then extract the text within.它基本上将字符串作为 HTML 插入到<p>元素中,然后提取其中的文本。

Edit: I just realize the JSP output you posted is not real HTML entities;编辑:我刚刚意识到您发布的 JSP output 不是真正的 HTML 实体; To process the example given you should use the following, add & before every #1234;要处理给出的示例,您应该使用以下内容,在每个#1234;之前添加& and make it &#1234;并使其成为&#1234; :

var str = $('<p />').html(str.replace(/\#(\d+)\;/g '&#$1;')).text();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM