gpt4 book ai didi

javascript - 解析 XML 提要时将 HTML 实体(例如 ')替换为等效字符

转载 作者:行者123 更新时间:2023-11-29 17:16:07 26 4
gpt4 key购买 nike

解析 XML 提要时,我从内容标签中获取文本,如下所示:

The Government has awarded funding for a major refurbishment project to go ahead at St Eunan’s College. This is in addition to last month’s announcement that grant for its prefabs to be replaced with permanent accomodation. The latest grant will allow for major refurbishment to a section of the school to allow for new accommodation for classes – the project will also involve roof repairs, the installation of a dust extraction system, new science room fittings and installation of firm alarms. Donegal Deputy Joe McHugh says credit must go to the school’s board of management

有没有办法轻松地将这些特殊字符(即 HTML 实体)(例如,撇号等)替换为它们的等效字符?

编辑:

Ti.API.info("is this real------------"+win.dataToPass)


返回:(为清楚起见添加了换行符)

[INFO][TiAPI   ( 5437)]  Is this real------------------Police in Strabane are
warning home owners and car owners in the town to be vigilant following a recent
spate of break-ins. There has been a number of thefts from gardens and vehicles
in the Jefferson Court and Carricklynn Avenue area of the town. The PSNI have
said that residents have reported seeing a dark haired male in and around the
area in the early hours of the morning. Local Cllr Karina Carlin has been
monitoring the situation – she says the problem seems to be getting
worse…….


我的 external.js 文件在下面,即只显示上面文本的文件:

var win= Titanium.UI.currentWindow;

Ti.API.info("Is this real------------------"+ win.dataToPass);

var escapeChars = { lt: '<', gt: '>', quot: '"', apos: "'", amp: '&' };

function unescapeHTML(str) {//modified from underscore.string and string.js
return str.replace(/\&([^;]+);/g, function(entity, entityCode) {
var match;

if ( entityCode in escapeChars) {
return escapeChars[entityCode];
} else if ( match = entityCode.match(/^#x([\da-fA-F]+)$/)) {
return String.fromCharCode(parseInt(match[1], 16));
} else if ( match = entityCode.match(/^#(\d+)$/)) {
return String.fromCharCode(~~match[1]);
} else {
return entity;
}
});
}

var newText= unescapeHTML(win.datatoPass);


var label= Titanium.UI.createLabel({
color: "black",
//text: win.dataToPass,//this works!
text:newText,//this is causing an error
font: "Helvetica",
fontSize: 50,
width: "auto",
height: "auto",
textAlign: "center"
})

win.add(label);

最佳答案

您可以在 Titanium 中包含许多库(Underscore.stringstring.js 可以实现这一点,但如果您只想要 unescape html 函数,只需尝试改编自上述库的这段代码

var escapeChars = { lt: '<', gt: '>', quot: '"', apos: "'", amp: '&' };

function unescapeHTML(str) {//modified from underscore.string and string.js
return str.replace(/\&([^;]+);/g, function(entity, entityCode) {
var match;

if ( entityCode in escapeChars) {
return escapeChars[entityCode];
} else if ( match = entityCode.match(/^#x([\da-fA-F]+)$/)) {
return String.fromCharCode(parseInt(match[1], 16));
} else if ( match = entityCode.match(/^#(\d+)$/)) {
return String.fromCharCode(~~match[1]);
} else {
return entity;
}
});
}

这会将那些特殊字符替换为人类可读的衍生字符,并返回修改后的字符串。只需将它放在代码中的某处,你就可以开始了,我自己在 Titanium 中使用过它,它非常方便。

关于javascript - 解析 XML 提要时将 HTML 实体(例如 ')替换为等效字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17678694/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com