简体   繁体   中英

escape quotes and single quote together in javascript

I have problem with escaping quotes and single quote together. I have a random string like this: Rome street "Cassia" 18 I use encodeUri to escape quotes

encodeUri('Rome street "Cassia" 18');

But i can also have a string like this: Rome stret 'Cassia' 18 I can't use same function becouse quote conflicting..

Help me!

 var data ='Rome street "Cassia" 18'; var escaped_data=data.replace(/["']/g, ""); $("#data").append(escaped_data); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <html> <p id="data"></p> <html> 

Assuming you want the String to be URI encoded you could try this:

encodeURI(myString.replace(/[']/g, '"'))

 var myInput = document.getElementById("myInput"), myOutput = document.getElementById("myOutput"); myInput.addEventListener("keyup", function () { myOutput.innerHTML = encodeURI(myInput.value.replace(/[']/g, '"')); }, false); 
 <input id="myInput"> <p id="myOutput"></p> 

Should you want to remove them completely the answer Yuvraj Mudaliar gave should help.

resolved!!

var String = encodeURIComponent(/[TEXT]/);
String = String.replace("%2F%5B",""); //replace /[
String = String.replace("%5D%2F",""); //replace ]/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM