Single and double quotes in JavaScript for error reporting

  • 2020-03-30 03:47:23
  • OfStack

When JavaScript is used to display messages or pass character data, it is common to encounter single quotes (') or double quotes (") in the data, which often cause JavaScript errors. The solution is usually /' or /".

Such as:


Alert("this is test "message"!"); 
Alert('this is test 'message'!');

This will be changed to the following statement


Alert("this is test /"message/"!"); 
// or  
Alert("this is test 'message'!"); 
Alert('this is test /'message/'!');

If the above is used only in scripts or without too complex data character concatenation, this problem has been solved.

However, if there are more complex data character concatenation, especially some JavaScript statements combined by the server, you can consider single quotes (') and double quotes ("), with escape sequence output.

For example, the above statement can be converted to the following format:


Alert("this is test /u0022message/u0022!"); 
Alert('this is test /u0027message/u0027!');

Add some common codes

The character specifies Unicode escape sequences
Long dash (--) /u2014
Registration symbol (R) /u00AE
Copyright symbol (c) /u00A9
Trademark symbol (?) / u2122
Euro symbol (wq /u20AC)
Backslash (/) /u005C
Forward slash (/) /u002F
Open curly brace ({) /u007B
Close curly brace (}) /u007D
Less than the number (<) / u003C
Greater than the number (>) / u003E
An asterisk (*)/u002A
& & amp;
'& apos; (/ u0027)
"& quot; (/ u0022)
< <
> >


Related articles: