How to replace "using" with javascript

I have a text box that sends information to the server and in JSON format. Suppose I want to enter two quotation marks for a value, and the JSON structure will look like this:

{ "test": """" } 

I need it to look like this:

 { "test": "\"\"" } 

therefore, it will follow JSON standards and can be collapsible / strict.

I tried to use

  var val = myVal.replace('"', "\\\""); 

but it didn’t work. val ends with just one escaped quote like \"" Any help is much appreciated!

+4
source share
4 answers

Some assumptions were made in my answer, since I had to fill in quite significant gaps in your question:

  • The user enters a text string into a text field;
  • Your script will read the contents of the text field and use this content as the value of one of the elements of the JSON string that it creates;
  • The script sends this received JSON string to the server in some way.

If I succeed, let me continue ...


Source

So with some placeholders you do:

 function get_contents_of_textbox() { // Dummy data for example return 'My mum pushed and I said "Hello World"!'; } function send_to_server(json_str) { // Dummy action: display JSON string console.log(json_str); } var myVal = get_contents_of_textbox(); var JSON = '{ "test": "' + myVal + '" }'; send_to_server(JSON); 

Live demo showing the wrong JSON.


Initial attempt

To ensure that JSON valid, avoid any quotation marks or backslashes that may contain. You have already given it:

 myVal = myVal.replace('"', "\\\""); 

and the result of your attempt :

 { "test": "My mum pushed and I said \"Hello World"!" } 

Only the first quote was screened. This is because by default only one instance of the search string is replaced.

Mozilla documentation says :

To perform a global search and replace, either include the g flag in the regular expression, or if the first parameter is a string, include g in the flags parameter.


Working attempt

Unfortunately, the flags parameter is non-standard, so go to the replace regular expression version and use the /g switch in it:

 myVal = myVal.replace(/"/g, '\\"'); 

(You will notice that I also shortened the replacement string for short.)

Result:

 { "test": "My mum pushed and I said \"Hello World\"!" } 

Live demo. Hooray!


Complete solution

Let it also add logic to discard backslashes, and in the end we get:

 function get_contents_of_textbox() { // Dummy data for example return 'My mum pushed [a back\\slash] and I said "Hello World"!'; } function send_to_server(json_str) { // Dummy action: display JSON string console.log(json_str); } var myVal = get_contents_of_textbox(); myVal = myVal.replace(/\\/g, '\\\\'); // escape backslashes myVal = myVal.replace(/"/g, '\\"'); // escape quotes var JSON = '{ "test": "' + myVal + '" }'; send_to_server(JSON); 

Result:

 { "test": "My mum pushed [a back\\slash] and I said \"Hello World\"!" } 

demo version.

+6
source

Use this

 var val = myVal.replace(/"/g, '\\"'); 
+3
source

Just create a JS array

 var arr = { test: '""' } 

and let the JSON interface handle you. You will never have to deal with the details yourself.

0
source

As you said, you get a string from a text field, you do not need to do anything to return it to the server. If you get the value of the text field via jQuery $() or pure document.getElementById , it will already be escaped correctly. JavaScript does not accept the value of your text field and tries to put it in quotation marks.

If "" entered in the text box, document.getElementById('mytextbox').value not equal to "\"\"" . Backslashes are only necessary when you define a string literal.

Try to remove all your escaping and regex and just send data from the text field without doing anything for it. Your json should look something like this:

 { "test" : document.getElementById('myTextBox') } 
0
source

All Articles