escaping - How to escape double(single) quotes inside another double(single) quotes with JavaScript -


i have input field user can enter ("", ''). there way can escape quotes without knowing type? mean if user enter ("test"test'test") how can store in js variable ? suggestion can me.

for example, want work properly:

<!doctype html> <html>   <body>      <script>        var myvar = "test"test'test";       console.log(myvar.replace('\"', '\\"'));      </script>    </body> </html> 

incorrect (with comments explaining why):

you cannot replace on syntactically broken variable declaration. need fix broken syntax.

<!doctype html> <html>   <body>     <script>         // <-- cannot this. it's incorrect syntax. need escape appropriately using backslashes.         var myvar = "test"test'test";          // line not required. need correct syntax error above instead.         console.log(myvar.replace('\"', '\\"'));     </script>    </body> </html> 

corrected:

here can see correctly escaped values inside string literal.

<!doctype html> <html>   <body>     <script>         var myvar = "test\"test\'test";         console.log(myvar);     </script>    </body> </html> 

Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -