当我解析XML时,它包含异常的十六进制字符。所以我试着用空格来代替它。但它根本不起作用。
原创角色:�
hex code : (253, 255)代码:
xmlData = String.replace(String.fromCharCode(253,255)," ");
retrun xmlData;我想从描述中删除“斧头”字符。有没有人在将十六进制字符替换为空格时有困难?
根据答案,我将代码修改如下:
testData = String.fromCharCode(253,255);
xmlData = xmlData.replace(String.fromCharCode(253,255), " ");
console.log(xmlData);但它仍然在屏幕上显示'�‘。
你知道为什么这种情况还会发生吗?
发布于 2012-06-21 18:56:58
字符代码实际上是255 * 256 + 253 = 65533,所以你会得到这样的结果:
xmlData = xmlData.replace(String.fromCharCode(65533)," ");字符串String.fromCharCode(253,255)由两个字符组成。
发布于 2012-06-21 18:01:06
您应该在String之外的string实例上调用replace()
var testData = String.fromCharCode(253,255);
var xmlData = testData.replace(String.fromCharCode(253,255), " ");
alert(xmlData);http://jsfiddle.net/StURS/2/的工作示例
发布于 2016-07-09 16:46:52
我刚刚遇到了这个问题,一个混乱的SQL-dump包含了有效的UTF-8代码和无效的UTF-8代码,强制进行更多的手动转换。由于上面的例子没有解决替换和寻找更好的匹配,我认为我在这里为那些正在与类似的编码问题作斗争的人提供了我的两点意见。以下代码:
控制根据查询解析我的sql-dump
替换的字符串
"use strict";
const readline = require("readline");
const fs = require("fs");
var fn = "my_problematic_sql_dump.sql";
var lines = fs.readFileSync(fn).toString().split(/;\n/);
const Aring = new RegExp(String.fromCharCode(65533) +
"\\" + String.fromCharCode(46) + "{1,3}", 'g');
const Auml = new RegExp(String.fromCharCode(65533) +
String.fromCharCode(44) + "{1,3}", 'g');
const Ouml = new RegExp(String.fromCharCode(65533) +
String.fromCharCode(45) + "{1,3}", 'g');
for (let i in lines){
let l = lines[i];
for (let ii = 0; ii < l.length; ii++){
if (l.charCodeAt(ii) > 256){
console.log("\n Invalid code at line " + i + ":")
console.log("Code: ", l.charCodeAt(ii), l.charCodeAt(ii + 1),
l.charCodeAt(ii + 2), l.charCodeAt(ii + 3))
let core_str = l.substring(ii, ii + 20)
console.log("String: ", core_str)
core_str = core_str.replace(/[\r\n]/g, "")
.replace(Ouml, "Ö")
.replace(Auml, "Ä")
.replace(Aring, "Å")
console.log("After replacements: ", core_str)
}
}
}生成的输出将如下所示:
Invalid code at line 18:
Code: 65533 45 82 65533
String: �-R�,,LDRALEDIGT', N
After replacements: ÖRÄLDRALEDIGT', N
Invalid code at line 18:
Code: 65533 44 44 76
String: �,,LDRALEDIGT', NULL
After replacements: ÄLDRALEDIGT', NULL
Invalid code at line 19:
Code: 65533 46 46 46
String: �...ker med fam till
After replacements: Åker med fam till我发现了一些值得注意的事情:
因此,{1,3}
Aring包含一个\\
,即匹配任何内容并需要额外的.
https://stackoverflow.com/questions/11135554
复制相似问题