Skip to content

Instantly share code, notes, and snippets.

@tablatronix
Last active April 5, 2018 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tablatronix/cb9043d0a8075d475d70725069b23789 to your computer and use it in GitHub Desktop.
Save tablatronix/cb9043d0a8075d475d70725069b23789 to your computer and use it in GitHub Desktop.
html excape examples
<script>
// https://stackoverflow.com/questions/24816/escaping-html-strings-with-jquery
escaped = new Option(unescaped).innerHTML;
var entityMap = {
'&': '&amp;',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
"'": '&#39;',
'/': '&#x2F;',
'`': '&#x60;',
'=': '&#x3D;'
};
function escapeHtml (string) {
return String(string).replace(/[&<>"'`=\/]/g, function (s) {
return entityMap[s];
});
}
function escapeHtmlEntities (str) {
if (typeof jQuery !== 'undefined') {
// Create an empty div to use as a container,
// then put the raw text in and get the HTML
// equivalent out.
return jQuery('<div/>').text(str).html();
}
// No jQuery, so use string replace.
return str
.replace(/&/g, '&amp;')
.replace(/>/g, '&gt;')
.replace(/</g, '&lt;')
.replace(/"/g, '&quot;');
}
var escapedHtml = html.replace(/&/g, '&amp;')
.replace(/>/g, '&gt;')
.replace(/</g, '&lt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&apos;');
function encode(e){return e.replace(/[^]/g,function(e){return"&#"+e.charCodeAt(0)+";"})}
function escapeHtml(text) {
'use strict';
return text.replace(/[\"&<>]/g, function (a) {
return { '"': '&quot;', '&': '&amp;', '<': '&lt;', '>': '&gt;' }[a];
});
}
function escapeHtml(text) {
'use strict';
return text.replace(/[\"&'\/<>]/g, function (a) {
return {
'"': '&quot;', '&': '&amp;', "'": '&#39;',
'/': '&#47;', '<': '&lt;', '>': '&gt;'
}[a];
});
}
</script>
String escapeParameter(String param) {
param.replace("+"," ");
param.replace("%21","!");
param.replace("%23","#");
param.replace("%24","$");
param.replace("%26","&");
param.replace("%27","'");
param.replace("%28","(");
param.replace("%29",")");
param.replace("%2A","*");
param.replace("%2B","+");
param.replace("%2C",",");
param.replace("%2F","/");
param.replace("%3A",":");
param.replace("%3B",";");
param.replace("%3D","=");
param.replace("%3F","?");
param.replace("%40","@");
param.replace("%5B","[");
param.replace("%5D","]");
return param;
}
String urlencode(String str)
{
String encodedString="";
char c;
char code0;
char code1;
char code2;
for (int i =0; i < str.length(); i++){
c=str.charAt(i);
if (c == ' '){
encodedString+= '+';
} else if (isalnum(c)){
encodedString+=c;
} else{
code1=(c & 0xf)+'0';
if ((c & 0xf) >9){
code1=(c & 0xf) - 10 + 'A';
}
c=(c>>4)&0xf;
code0=c+'0';
if (c > 9){
code0=c - 10 + 'A';
}
code2='\0';
encodedString+='%';
encodedString+=code0;
encodedString+=code1;
//encodedString+=code2;
}
yield();
}
return encodedString;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment