1

I try to replace "\" character to empty "" but I can't do it. How to solve it?

Here is my code

abc = abc.replace('\','');
Cœur
  • 37,241
  • 25
  • 195
  • 267
Chan Yoong Hon
  • 1,592
  • 7
  • 30
  • 71
  • What about marking the answer of @Tim as the right one if it does solve the problem? – lin Feb 15 '17 at 10:42

2 Answers2

8

to replace all occurrences in a string you need to use a regex with the g flag,

abc = abc.replace(/\\/g, '');
Benja
  • 4,099
  • 1
  • 30
  • 31
2

I think you need to escape the backslash:

abc = abc.replace('\\', '');

If you want to replace all occurrences, you can try:

var pattern = '\\\\';
var re = new RegExp(pattern, 'g');
abc = abc.replace(re, '');
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360