jeum
jeum

Reputation: 1096

replace a pattern by \x

I have strings in which accented characters are coded in hex like this :

extrémité > extr\X2\00E9\X0\mit\X2\00E9\X0\

So I plan to replace \X2\00(charcode)\X0\ by \X(charcode) but I can't write '\x' in :

out.replace(/\X2\00/g,'\x');

How can I do that ?


This doesn't work neither :

out.replace(/2\00/g,'');

This is for a parser, here is a file line :

#242= IFCPROPERTYSINGLEVALUE('Num\X2\00E9\X0\ro',$,IFCLABEL('5'),$);

Upvotes: 0

Views: 625

Answers (1)

trincot
trincot

Reputation: 350705

You need to escape backslashes in JavaScript regular expressions and strings.

From your comments I understand that your source data (in a file) looks like this:

\X2\00E9\X0\

Note that if you want to reproduce this data (for testing), in the console, you need to escape those backslashes. In JavaScript notation the above data is represented as '\\X2\\00E9\\X0\\'.

Also, to generate the accented letters you could use charFromCode(), and use replace() with a callback function:

// 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);

Upvotes: 3

Related Questions