我有一些字符串,其中重音字符以十六进制编码,如下所示:
extrémité > extr\X2\00
E9\X0\
mit\X2\00
E9\X0\
所以我打算将 \X2\00
(charcode)\X0\
替换为 \X
<强>(字符码)
但我不能在 :
'\x'
out.replace(/\X2\00/g,'\x');
我怎样才能做到这一点?
<小时/>这也不起作用:
out.replace(/2\00/g,'');
<小时/>
这是一个解析器,这是一个文件行:
#242= IFCPROPERTYSINGLEVALUE('Num\X2\00E9\X0\ro',$,IFCLABEL('5'),$);
最佳答案
您需要转义 JavaScript 正则表达式和字符串中的反斜杠。
根据您的评论,我了解到您的源数据(在文件中)如下所示:
\X2\00E9\X0\
请注意,如果您想在控制台中重现此数据(用于测试),则需要转义这些反斜杠。在 JavaScript 表示法中,上述数据表示为 '\\X2\\00E9\\X0\\'
。
此外,要生成重音字母,您可以使用 charFromCode()
,并将 replace()
与回调函数一起使用:
// note that in JS strings, backslashes need to be escaped to get
// the text as it appears in your file. This is just to mimic the file input
var str = 'extr\\X2\\00E9\\X0\\mit\\X2\\00E9\\X0\\';
// .. and also in JS regexes, the backslashes need to be escaped.
str = str.replace(/\\X2\\00(..)\\X0\\/g, function(_, match) {
// match is now the two letter hex code, convert to number and then
// to character, and return it as replacement
return String.fromCharCode(parseInt(match,16));
});
document.write(str);
关于javascript - 将模式替换为\x,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37400805/