0

I have string and there are alot of character which is "\" so I want to remove all of them but when I use

string.replace("\\","");

it removes just first character. There is no replaceAll in reactnative. How can I do that ?

jo_va
  • 13,504
  • 3
  • 23
  • 47
rnn
  • 2,393
  • 3
  • 21
  • 39

2 Answers2

3

Use regex in your replace.

Example:

const string = "aaabbbcccaaa";

// Removes first "a" only
console.log(string.replace("a", ""));

// Removes all "a"
console.log(string.replace(/a/g, ""));
Gary Thomas
  • 2,291
  • 1
  • 9
  • 21
2

You have to use a regex with the g modifier whichs means globally, that will apply the regex as many times as necessary. You can use a tool like this to build and test your regex.

console.log('hello\\world\\'.replace(/\\/g, ''));
jo_va
  • 13,504
  • 3
  • 23
  • 47