Hướng dẫn javascript escape special characters

Take your JSON and .stringify() it. Then use the .replace() method and replace all occurrences of \n with \\n.

EDIT:

As far as I know of, there are no well-known JS libraries for escaping all special characters in a string. But, you could chain the .replace() method and replace all of the special characters like this:

var myJSONString = JSON.stringify(myJSON);
var myEscapedJSONString = myJSONString.replace(/\\n/g, "\\n")
                                      .replace(/\\'/g, "\\'")
                                      .replace(/\\"/g, '\\"')
                                      .replace(/\\&/g, "\\&")
                                      .replace(/\\r/g, "\\r")
                                      .replace(/\\t/g, "\\t")
                                      .replace(/\\b/g, "\\b")
                                      .replace(/\\f/g, "\\f");
// myEscapedJSONString is now ready to be POST'ed to the server. 

But that's pretty nasty, isn't it? Enter the beauty of functions, in that they allow you to break code into pieces and keep the main flow of your script clean, and free of 8 chained .replace() calls. So let's put that functionality into a function called, escapeSpecialChars(). Let's go ahead and attach it to the prototype chain of the String object, so we can call escapeSpecialChars() directly on String objects.

Like so:

String.prototype.escapeSpecialChars = function() {
    return this.replace(/\\n/g, "\\n")
               .replace(/\\'/g, "\\'")
               .replace(/\\"/g, '\\"')
               .replace(/\\&/g, "\\&")
               .replace(/\\r/g, "\\r")
               .replace(/\\t/g, "\\t")
               .replace(/\\b/g, "\\b")
               .replace(/\\f/g, "\\f");
};

Once we have defined that function, the main body of our code is as simple as this:

var myJSONString = JSON.stringify(myJSON);
var myEscapedJSONString = myJSONString.escapeSpecialChars();
// myEscapedJSONString is now ready to be POST'ed to the server

Table of Contents

  • Escape JSON string in JavaScript
  • Conclusion
    • Was this post helpful?

In this post, we will see how to escape JSON string containing newline characters using JavaScript.

There is no well known JavaScript library which can escape all special characters in String.

There is no direct way to escape JSON String in JavaScript.

You could however chain string's replace() method and replace all special characters using a custom function.

Here is one of way to do it.

varjsonStr=JSON.stringify(jsonStr);

varescapedJSONString=jsonStr .replace(/[\\]/g,'\\\\')

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

                                .replace(/[\/]/g,'\\/')

                                .replace(/[\b]/g, '\\b')

                                .replace(/[\f]/g,'\\f')

                                .replace(/[\n]/g, '\\n')

                                .replace(/[\r]/g,'\\r')

                                .replace(/[\t]/g, '\\t');

console.log(escapedJSONString);

You can create a custom function such as escapeSpecialCharsInJSONString() as below:

String.prototype.escapeSpecialCharsInJSONString=function(){

    returnthis .replace(/[\\]/g,'\\\\')

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

                .replace(/[\/]/g,'\\/')

                .replace(/[\b]/g, '\\b')

                .replace(/[\f]/g,'\\f')

                .replace(/[\n]/g, '\\n')

                .replace(/[\r]/g,'\\r')

                .replace(/[\t]/g, '\\t');

};

Let’s see with the help of example.

String.prototype.escapeSpecialCharsInJSONString=function(){

    returnthis .replace(/[\\]/g,'\\\\')

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

                .replace(/[\/]/g,'\\/')

                .replace(/[\b]/g, '\\b')

                .replace(/[\f]/g,'\\f')

                .replace(/[\n]/g, '\\n')

                .replace(/[\r]/g,'\\r')

                .replace(/[\t]/g, '\\t');

};

consttext='{"name":"John\n", "birth":"14/12/1989\t"}';

constresult=text.escapeSpecialCharsInJSONString();

console.log(result);

Output

{\"name\":\"John\n\",\"birth\":\"14\/12\/1989\t\"}

As you can see, we have successfully escaped new line character and tab character from the JSON String.

Conclusion

To escape JSON string containing newline characters, chain replace() method with all required special characters which you want to escape.

That’s all about how to escape JSON string containing newline characters in JavaScript.