0

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?

dcsan
  • 11,333
  • 15
  • 77
  • 118

1 Answers1

1

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');
Eugene Tsakh
  • 2,777
  • 2
  • 14
  • 27
  • OK thats a good point. I do have source content which has those glitches in, I can see in the txt files.I was trying to reproduce that data in a simple test case... but creating the test content I screwed up doh! – dcsan Oct 30 '18 at 20:42
  • any thoughts how I could create a string with the bogus characters in from raw JS? – dcsan Oct 30 '18 at 20:43
  • @dcsan try to escape backslashes `"生病以后,爸爸\\_什么\\_酒\_都\\_不\\_能喝了"` ) – Eugene Tsakh Oct 30 '18 at 20:58