How to convert characters to HTML entities using plain JavaScript

With the help of bucabay and the advice to create my own function i created this one which works for me. What do you guys think, is there a better solution somewhere? if(typeof escapeHtmlEntities == ‘undefined’) { escapeHtmlEntities = function (text) { return text.replace(/[\u00A0-\u2666<>\&]/g, function(c) { return ‘&’ + (escapeHtmlEntities.entityTable[c.charCodeAt(0)] || ‘#’+c.charCodeAt(0)) + ‘;’; }); … Read more

In Java, is there a way to write a string literal without having to escape quotes?

No, and I’ve always been annoyed by the lack of different string-literal syntaxes in Java. Here’s a trick I’ve used from time to time: String myString = “using `backticks` instead of quotes”.replace(‘`’, ‘”‘); I mainly only do something like that for a static field. Since it’s static the string-replace code gets called once, upon initialization … Read more

How to escape single quote in sed?

Quote sed codes with double quotes: $ sed “s/ones/one’s/”<<<“ones thing” one’s thing I don’t like escaping codes with hundreds of backslashes – hurts my eyes. Usually I do in this way: $ sed ‘s/ones/one\x27s/'<<<“ones thing” one’s thing

Batch character escaping

As dbhenham points out in this comment, a (MUCH) more detailed answer can be found in portions of this answer (originally by another user jeb and significantly edited and updated by dbhenham since) on a related but much more general question: parsing – How does the Windows Command Interpreter (CMD.EXE) parse scripts? – Stack Overflow … Read more

How do I .decode(‘string-escape’) in Python 3?

You’ll have to use unicode_escape instead: >>> b”\\123omething special”.decode(‘unicode_escape’) If you start with a str object instead (equivalent to the python 2.7 unicode) you’ll need to encode to bytes first, then decode with unicode_escape. If you need bytes as end result, you’ll have to encode again to a suitable encoding (.encode(‘latin1’) for example, if you … Read more

How to escape strings in SQL Server using PHP?

addslashes() isn’t fully adequate, but PHP’s mssql package doesn’t provide any decent alternative. The ugly but fully general solution is encoding the data as a hex bytestring, i.e. $unpacked = unpack(‘H*hex’, $data); mssql_query(‘ INSERT INTO sometable (somecolumn) VALUES (0x’ . $unpacked[‘hex’] . ‘) ‘); Abstracted, that would be: function mssql_escape($data) { if(is_numeric($data)) return $data; $unpacked … Read more