Good evening.

How in javascript can I replace all the characters with the ones I need? I pull out albums from Facebook, and it allows me to write ' and ' in the title of the album. Naturally on my website this results in a problem. That's what I tried:

function clearFromSlashes(str){ var slashes1="\'"; //заменяем одинарные кавычки var slashes2="\""; //заменяем двойные кавычки var newStr=str.replace(slashes1," "); return newStr.replace(slashes2," "); } 

This function replaces only the very first quotes, if further in the title quotes are found again, the function skips them. How to catch them too? And .. as you see, I made 2 variables slashes1 and slashes1, for some reason, if I wrote like this:

 var slashes1="\',\""; - оно не работало.. не знаю почему 
  • the campaign needs to be done through the do while, but how do you know how many single or double quotes in the text? - Denis Masster 10:02 pm

2 answers 2

replace uses a regular expression, not a string.

 str.replace(/'|"/g,'') 
  • yeah, thanks I will know, I had already built such a great atom)) var slashes1 = "\ '"; var slashes2 = "\" "; var one = str.split (slashes1) .length-1; var two = str.split (slashes2) .length-1; do {str = str.replace (slashes1," "); one--;} while (one> 0) do {str = str.replace (slashes2, ""); two--;} while (two> 0) - Denis Masster

At least the old-fashioned way:

 function htmlQuotes(str) { var replace = [ '\'': '&quot;', '\"': '&quot;', '<' : '&lt;', '>' : '&gt;' // и так далее ]; for (var n in replace) str = str.split(n).join(replace[n]); return str; }