SoFunction
Updated on 2025-03-09

Solution to report errors in single and double quotes in JavaScript

When using JavaScript to display messages or pass character data, you often encounter single quotes (') or double quotes (") in the data. Such statements often cause JavaScript to report errors. This is generally solved by /' or /".

For example:

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

It will usually be changed to the following statement

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

This problem has been solved if the above is only used in scripts or does not have too complex data character connections.

However, if there are relatively complex data character connections, especially some JavaScript statements combined by the server, you can consider using single quotes (') and double quotes (") to output them with escape sequences.

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 encodings

Character Description Unicode escape sequence
Long dash (—) /u2014
Registration symbol (R) /u00AE
Copyright symbol (c) /u00A9
Trademark Symbol (?) /u2122
Euro symbol (€) /u20AC
Backslash (/) /u005C
Forward slash (/) /u002F
Open braces ({) /u007B
Close braces (}) /u007D
Less than (<) /u003C
Greater than (>) /u003E
Asterisk (*) /u002A
& &amp;
' &apos;(/u0027)
" &quot;(/u0022)
< &lt;
> &gt;