STRING::ESCAPE_JAVASCRIPT
1g 2024-05-08 laplante@plcb.ca GOWEB/STRING — Escape Strings for JavaScripttitle: “STRING::ESCAPE_JAVASCRIPT” version: “1.0.0” date: 2024-05-08 author: “laplante@plcb.ca” section: “1g” category: “GOWEB/STRING”
string::escape_javascript Escape Strings for JavaScript
string::escape_javascript(string [, quote: true])
escape_javascript(string [, quote: true])
string::escapeJavascript(string [, quote: true])
escapeJavascript(string [, quote: true])
The escape_javascript
function escapes a string for safe use within JavaScript code.
It ensures that special characters are properly escaped to prevent syntax errors or injection issues.
Characters escaped include:
"
) → \"
\
) → \\
\n
, \r
, \t
, etc.\uXXXX
(where XXXX
is the Unicode code point in hexadecimal)If the quote
parameter is true
(default), the returned string is wrapped in double quotes.
string (string, required) The text to escape.
quote (boolean, optional)
If true
(default), enclose the escaped output in double quotes.
If false
, return only the escaped string without wrapping quotes.
res={{
a := qq("'\n\t\rabcéé");
escape_javascript(a); "\n";
a := "<script>console.log('allo');";
escape_javascript(a); "\n";
a := "'test'";
escape_javascript(a); "\n";
escape_javascript(quote:false, a); "\n";
}};
Returns:
res = "\"'\n\t\rabcéé"
"<script>console.log('allo');"
"'test'"
'test'