| 1 |
"use strict"; |
| 2 |
|
| 3 |
function hex(ch) { return ch.charCodeAt(0).toString(16).toUpperCase(); } |
| 4 |
|
| 5 |
|
| 6 |
var js = { |
| 7 |
stringEscape: function(s) { |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
return s |
| 19 |
.replace(/\\/g, '\\\\') |
| 20 |
.replace(/"/g, '\\"') |
| 21 |
.replace(/\0/g, '\\0') |
| 22 |
.replace(/\x08/g, '\\b') |
| 23 |
.replace(/\t/g, '\\t') |
| 24 |
.replace(/\n/g, '\\n') |
| 25 |
.replace(/\f/g, '\\f') |
| 26 |
.replace(/\r/g, '\\r') |
| 27 |
.replace(/[\x00-\x0F]/g, function(ch) { return '\\x0' + hex(ch); }) |
| 28 |
.replace(/[\x10-\x1F\x7F-\xFF]/g, function(ch) { return '\\x' + hex(ch); }) |
| 29 |
.replace(/[\u0100-\u0FFF]/g, function(ch) { return '\\u0' + hex(ch); }) |
| 30 |
.replace(/[\u1000-\uFFFF]/g, function(ch) { return '\\u' + hex(ch); }); |
| 31 |
}, |
| 32 |
|
| 33 |
regexpClassEscape: function(s) { |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
return s |
| 40 |
.replace(/\\/g, '\\\\') |
| 41 |
.replace(/\//g, '\\/') |
| 42 |
.replace(/\]/g, '\\]') |
| 43 |
.replace(/\^/g, '\\^') |
| 44 |
.replace(/-/g, '\\-') |
| 45 |
.replace(/\0/g, '\\0') |
| 46 |
.replace(/\t/g, '\\t') |
| 47 |
.replace(/\n/g, '\\n') |
| 48 |
.replace(/\v/g, '\\x0B') |
| 49 |
.replace(/\f/g, '\\f') |
| 50 |
.replace(/\r/g, '\\r') |
| 51 |
.replace(/[\x00-\x0F]/g, function(ch) { return '\\x0' + hex(ch); }) |
| 52 |
.replace(/[\x10-\x1F\x7F-\xFF]/g, function(ch) { return '\\x' + hex(ch); }) |
| 53 |
.replace(/[\u0100-\u0FFF]/g, function(ch) { return '\\u0' + hex(ch); }) |
| 54 |
.replace(/[\u1000-\uFFFF]/g, function(ch) { return '\\u' + hex(ch); }); |
| 55 |
} |
| 56 |
}; |
| 57 |
|
| 58 |
module.exports = js; |