kosbou
kosbou

Reputation: 3999

js string replace does not work

I am trying to replace ',",\ characters with blank.

I use the following code

jQuery.get('/path to file/file.txt',function(data){
  data = data.replace(/\'\"\\/g," ");
  alert(data);
})

but nothing replaced.

Where am I wrong?

Upvotes: 2

Views: 163

Answers (2)

Michael Berkowski
Michael Berkowski

Reputation: 270637

Your expression would replace the 3 character sequence '"\ with a space, not the individual chars \, ' and ". Enclose them in a character class [] (and there's no need to escape with \ except for the \).

data = data.replace(/['"\\]/g," ");

// Example:
var str = "This string has internal \" double \\ quotes \\ and 'some single ones' and a couple of backslashes";
str = str.replace(/['"\\]/g," ");
// Output: all quotes replaced with spaces:
// "This string has internal   double   quotes   and  some single ones  and a couple of backslashes"

Upvotes: 1

GottZ
GottZ

Reputation: 4947

you are wrong. there is nothing to replace. regex does not open any file. all it does is replacing the char combination '" with whitespace.

what you search for is ['"] instead of \'\"

Upvotes: 1

Related Questions