dcsan
dcsan

Reputation: 12275

replacing JS backslashes in unicode text

I'm having a tough time trying to replace some \_ in some text in JS. I've tried various combinations with fromCharCode and split but cannot seem to find success.

In all these cases the output is identical to the input, I can't rip out or replace the junk characters. It seems the backslash-underscore is invisible to JS. Wondering if it's related to the string being unicode?

Suggestions appreciated!

let v1 = {
  s: "生病以后,爸爸\_什么\_酒\_都\_不\_能喝了"
}

let v2 = { ...v1 }  // copy
let v3 = { ...v2 }  // copy

v2.s = v2.s.replace(/\\/g, "X")
v3.s = v3.s.split(String.fromCharCode(92)).join("Y")

console.log("v1", v1)
console.log("v2", v2)
console.log("v3", v3)

At this point I might mess with a sed script ;.;

related checked do not solve: Javascript and backslashes replace Replace back slash (\) with forward slash (/) Converting backslashes into forward slashes using javascript does not work properly?

Upvotes: 0

Views: 447

Answers (1)

Eugene Tsakh
Eugene Tsakh

Reputation: 2879

When you are assigning your string like this:

let v1 = {
  s: "生病以后,爸爸\_什么\_酒\_都\_不\_能喝了"
}

All backslashes will be removed and your result string will be 生病以后,爸爸_什么_酒_都_不_能喝了

So you have to escape those backslashes as well:

let v1 = {
  s: "生病以后,爸爸\\_什么\\_酒\\_都\\_不\\_能喝了"
}

And now to achieve your expected result you can use next replace regexp:

v1.s = v1.s.replace(/\\_/g, 'X');

Upvotes: 1

Related Questions